首页 > 解决方案 > 当应用程序被用户或后台应用程序杀死时,活动未启动单击 FCM 推送通知

问题描述

我正在设置 FCM 推送通知,当我杀死应用程序或后台应用程序并生成通知时,当我单击通知时生成通知,然后应用程序未打开,Firebase 也没有给出任何日志错误。

通知通过 rest api post 请求生成到 firebase 后端。

我还通过 FCM Notification Quick Project 检查了同样的问题。

注意:-> 默认情况下,用户点击通知会打开应用程序启动器。一旦默认活动打开,这将不起作用,然后我将从系统托盘获取数据。

https://github.com/firebase/quickstart-android/issues/765

https://firebase.google.com/docs/cloud-messaging/android/receive#backgrounded

标签: androidfirebaseandroid-intentpush-notificationbackground

解决方案


private NotificationManager mNotificationManager;
        private static int NOTIFICATION_ID = 0; 

private void sendNotification(String msg) {

        PendingIntent contentIntent = null;
        mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

        contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                .setContentTitle("Title")
                .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                .setContentText(msg);

        mBuilder.setContentIntent(contentIntent);
        mBuilder.setAutoCancel(true);

        Random random = new Random();
        NOTIFICATION_ID = random.nextInt(9999 - 1000) + 1000;
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }

推荐阅读