【问题标题】:FCM Multiple push notifications not working properly when app in background state当应用程序处于后台状态时,FCM 多个推送通知无法正常工作
【发布时间】:2017-06-07 05:30:43
【问题描述】:

我将 fcm 用于推送通知,我只处理数据负载。当应用程序处于前台时,多个通知工作正常,在这里我使用 click_action 中服务器的 id 值在我们点击它时将通知移动到相关帖子。但是,当应用程序处于后台/关闭状态时,每当我收到多个通知时,我将点击一个通知,它将转到相关帖子,但其余所有通知都不会重定向到相关帖子,只是从通知中清除。我不知道为什么会出现这个问题。这是我的代码

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";


@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    Log.d(TAG, "From: " + remoteMessage.getFrom());


        Log.d(TAG, "Message data payload: " + remoteMessage.getData());

sendNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("body"),remoteMessage.getData().get("click_action"),image);
        }

private void sendNotification(String title, String message, String id, String image) {
    Log.d("notificationdetails",title+",,,"+message+",,"+id);
    Intent  intent = null;



int postid = Integer.valueOf(id);
        if(id.equals("")|| id==null) {
            intent = new Intent(this, SplashActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        }

else{

        Constants.PushId = postid;
        intent = new Intent(this, DetailActivity.class);
        Log.d("mypostid",postid+"");
        intent.putExtra("id", postid);
        intent.putExtra("backpage","main");
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    }

    PendingIntent pendingIntent = PendingIntent.getActivity(this, postid, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher_primepost)
            .setContentTitle(title)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);


    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Random random = new Random();
    int num = random.nextInt(99999-1000)+1000;
    notificationManager.notify(num /* ID of notification */, notificationBuilder.build());
}

}

Manifest.xml

 <activity android:name="com.primepostnews.app.Activities.DetailActivity"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="Detail" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

    </activity>

【问题讨论】:

  • 如果存在相同的通知 ID,您正在使用 PendingIntent.FLAG_UPDATE_CURRENT 此更新通知数据。就像您使用随机数生成 id 一样。尝试使用不同的方法生成通知 ID 可能会有帮助。
  • 顺便说一句:通过 random.nextInt(99999-1000)+1000; 生成通知 ID 是个坏主意。请改用纪元。

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


【解决方案1】:

有两种类型的消息数据消息和通知消息。无论应用程序是在前台还是后台,数据消息都在 onMessageReceived 中处理。数据消息是传统上与 GCM 一起使用的类型。只有当应用程序在前台时,才会在 onMessageReceived 中接收通知消息。当应用程序在后台时,会显示一个自动生成的通知。当用户点击通知时,他们会返回到应用程序。包含通知和数据有效负载的消息被视为通知消息。 Firebase 控制台始终发送通知消息。

 if (remoteMessage.getData() != null && remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());

sendNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("body"),remoteMessage.getData().get("click_action"),image);

        } else if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());

sendNotification(remoteMessage.getNotification().getBody(), remoteMessage.getNotification().getBody(),remoteMessage.getData().get("click_action"),image);
        }

【讨论】:

    【解决方案2】:

    您应该使用通知类型的消息来处理所有情况。当应用程序被杀死/在后台时,数据类型不起作用。因为对于数据类型通知 OnMessageRecieved 将被调用,您必须使用 NotificationBuilder 手动创建状态栏通知。

    所以总是更好地使用来自服务器的通知类型输入,如下所示

    "notification": {
                "title": "Firebase notification",
                "message": "I am firebase notification. you can customise me. enjoy",
                "click_action": "OPEN_ACTIVITY",
                "sound":"default",
    
            }
    

    这在所有情况下都有效,而且您不必担心在代码中处理它。

    【讨论】:

      猜你喜欢
      • 2017-04-28
      • 2016-10-18
      • 1970-01-01
      • 1970-01-01
      • 2019-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多