应用程序被杀/刷出时的FCM推送通知

时间:2017-02-01 09:33:06

标签: android firebase push-notification firebase-cloud-messaging

我正在使用FCM云消息传递,当应用程序处于后台但未被杀死或应用程序运行时,它正常工作。 一旦应用程序停止或被最近的应用程序杀死,它就不会收到任何通知。

一旦应用程序启动,即使它没有收到旧消息。

onMessageReceived()的代码

 @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Map<String, String> data = remoteMessage.getData();
        String myCustomKey = data.get("Nick");
        String myCustomKey1 = data.get("Room");
        testNotification(myCustomKey,myCustomKey1);
}

private void testNotification(String message,String title) {
        Intent intent = new Intent(this, ChatActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);


        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.sham_icon)
                .setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

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

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

    }

用于发送通知的服务器端代码

{
    "to" : "token-key",
    "data" : {
        "title":"title",
        "body":"body",
      "Nick" : "Mario",
      "Room" : "PortugalVSDenmark"
    }
  } 

firebase服务和通知的清单代码

     <meta-data
 android:name="com.google.firebase.messaging.default_notification_icon"
                android:resource="@drawable/narendramodi_icon" />
                android:resource="@drawable/sham_icon" />

             <meta-data
                 android:name="com.google.firebase.messaging.default_notification_color"
                 android:resource="@color/accent" />

                  <service android:name=".firebase.MyFirebaseMessagingService"
                 android:exported="false">
                <intent-filter android:priority="10000">
                     <action android:name="com.google.firebase.MESSAGING_EVENT" />
                 </intent-filter>
             </service>
             <service android:name=".firebase.MyFirebaseInstanceIDService">
                 <intent-filter>
                     <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
                 </intent-filter>
             </service>

感谢任何帮助。

3 个答案:

答案 0 :(得分:1)

这是因为在某些设备上系统不允许其他应用启动应用或应用服务,对于FCM通知,如果您的应用被杀,那么Google Play服务会启动您的应用服务(例如小米有MIUI的手机。)

答案 1 :(得分:0)

使用您的应用服务器和FCM服务器API:仅设置数据密钥。可以是可折叠的,也可以是不可折叠的。

如果您要从控制台发送通知,请使用特定密钥从高级选项功能发送通知。

if (getIntent().getExtras() != null) {
     // Call your NotificationActivity here..
     Intent intent = new Intent(MainActivity.this, NotificationActivity.class);
     startActivity(intent);
    }

答案 2 :(得分:0)

Android框架建议,如果没有明确的用户交互,就不应启动已停止的应用程序(不在后台)。
FCM遵循此建议,因此不会向已停止的应用程序发送消息。
对于某些手机,一旦应用程序停止或被最近的应用程序杀死,它就完全被操作系统停止了。这可以节省电池,但fcm不起作用。

相关问题