【问题标题】:Activity not launch click on FCM push notification when app killed by user or App in background当应用程序被用户或后台应用程序杀死时,活动未启动单击 FCM 推送通知
【发布时间】:2019-05-21 18:46:07
【问题描述】:

我正在设置 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

【问题讨论】:

    标签: android firebase android-intent push-notification background


    【解决方案1】:
    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());
        }
    

    【讨论】:

    • 当应用程序在后台或被用户和用户杀死时点击通知然后 onMessageReceived(RemoteMessage remoteMessage) 这个方法不会调用并且你已经提供了放在 onMessageReceived(RemoteMessage remoteMessage) 方法里面的代码 这不起作用这个代码当应用程序打开并且用户点击通知时工作,但我不需要这个
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-22
    • 1970-01-01
    • 2020-02-10
    • 2017-12-02
    相关资源
    最近更新 更多