开发者社区> 问答> 正文

即使应用关闭,如何显示像真正的呼叫者一样的警报?

我是的developing SOS application,当我收到每个通知时,android app我想打开一个alert pop-up会显示发件人的一些详细信息SOS icons并continuous alert tone会响起的铃声,我正在使用下面的代码来完成,但是在android 9上活动不会开始。而start activity当app为时,此代码不会显示killed。


private void sendNotification(String messageBody, String notificationType, String notificationId) {       
            try {
                Intent intent = new Intent(this, SosPopUpActivity.class);

                intent.putExtra("message", messageBody);
                intent.putExtra("notification_type", notificationType);
                PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

                NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                    NotificationChannel mChannel = new NotificationChannel(
                            channelId, channelName, importance);
                    notificationManager.createNotificationChannel(mChannel);
                }


                Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                //  NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.drawable.ic_notification)
                        .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher))
                        .setContentTitle(getString(R.string.app_name))
                        .setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
                        .setContentText(messageBody)
                        .setAutoCancel(false)
                        .setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getApplicationContext().getPackageName() + "/" + R.raw.sos))

                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);


                if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    notificationBuilder.setSmallIcon(R.drawable.ic_notification);
                    notificationBuilder.setColor(getResources().getColor(R.color.colorPrimary));
                    //notificationBuilder.setColor(getResources().getColor(R.color.notification_color));
                } else {
                    notificationBuilder.setSmallIcon(R.drawable.ic_notification);
                }

                notificationBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);

                //  NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

                // notificationManager.notify(1234, notificationBuilder.build());

                TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
                stackBuilder.addNextIntent(intent);
                PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
                notificationBuilder.setContentIntent(resultPendingIntent);

                notificationManager.notify(1234, notificationBuilder.build());

                //startActivity(intent);
            }catch (Exception e)
            {
                e.printStackTrace();
                AlertDialogHelper.showAlertDialog(this,e.getLocalizedMessage());
            }

        }

*Please help* .

展开
收起
Puppet 2020-01-18 19:47:28 398 0
1 条回答
写回答
取消 提交回答
  • 可以发布日志吗?这样我们才能知道应用程序崩溃的确切位置

    2020-01-18 19:47:58
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载