首页 > 解决方案 > 即使应用程序关闭,如何像真正的来电者一样显示警报?

问题描述

我正在开发一个 SOS 应用程序。

当在 Android 应用程序中收到通知时,我想打开一个警报弹出窗口,其中将显示发件人和 SOS 图标的一些详细信息,并且还会响起持续的警报音。

我是通过使用下面的代码来做到这一点的,但是在 android 9 上,Activity 没有启动。当应用程序处于终止状态时,此代码不会启动活动,也不能在后台模式下工作。

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());
    }

}

标签: javaandroidfirebasefirebase-cloud-messaging

解决方案


推荐阅读