第二次推送通知无法打开我的应用



我正在使用FCM云消息服务,我可以一次从服务器生成一些通知。但唯一的问题是,我能够打开第一个通知,但是从第二个通知开始,直到其余通知在我单击它们时不会做任何事情。

消息服务代码 :

public class MyFirebaseMessagingService extends FirebaseMessagingService {
private final String NOTIFICATION_TYPE = "type";
private final String NOTIFICATION_ID = "id";
private final String NOTIFICATION_TYPE_PRODUCT_DETAIL = "productdetail";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    String title = remoteMessage.getNotification().getTitle();
    String body = remoteMessage.getNotification().getBody();
    String token = remoteMessage.getFrom();
    Log.d("FireBase TAG: ", token);
    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d("FireBaseMessageService","FireBase Data payload : " + remoteMessage.getData());
    }
    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    }

}

@Override
public void handleIntent(Intent intent) {
    String type = intent.getExtras().getString(NOTIFICATION_TYPE, "");
    int id = 0;
    try {
        id = Integer.valueOf(intent.getExtras().getString(NOTIFICATION_ID, ""));
    } catch (NumberFormatException e) {
        e.printStackTrace();
    }
    //Intents
    Intent mainIntent = MainActivity.newIntent(this);
    Intent editProfileIntent = EditProfileActivity.newIntent(this);
    Intent settingsIntent = SettingsActivity.newIntent(this);
    Intent productIntent = ProductActivity.newNotificationIntent(this, id, false, true);

    if (UserManager.getSingleton().isUserLoggedIn(getApplicationContext())) {
        PendingIntent pendingIntent;
        if (type.equalsIgnoreCase(NOTIFICATION_TYPE_PRODUCT_DETAIL)) {
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
            stackBuilder.addParentStack(MainActivity.class);
            stackBuilder.addNextIntent(mainIntent);
            stackBuilder.addNextIntent(productIntent);
            editProfileIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_ONE_SHOT);
        } else if (type.isEmpty() || type.equals("")){
            pendingIntent = PendingIntent.getActivity(this, 0, mainIntent,
                    PendingIntent.FLAG_ONE_SHOT);
        } else {
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
            stackBuilder.addParentStack(MainActivity.class);
            stackBuilder.addNextIntent(mainIntent);
            stackBuilder.addNextIntent(productIntent);
            pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_ONE_SHOT);

        }
        Bitmap icon = BitmapFactory.decodeResource(getResources(),
                R.drawable.onerm_logo_rounded);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.oneringgit_transparent)
                .setLargeIcon(icon)
                .setContentTitle(intent.getExtras().getString("gcm.notification.title"))
                .setContentText(intent.getExtras().getString("gcm.notification.body"))
                .setDefaults(NotificationCompat.DEFAULT_VIBRATE)
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .setColor(getBaseContext().getResources().getColor(R.color.colorPrimary));
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(id, builder.build());
    }
}

怀疑这与我放入管理器.notify (( 方法的"id"变量有关。

有没有办法解决这个问题?

是的更改id,因为它覆盖了之前的通知,因此不会打开任何内容,请将其更改为:

 int num = (int) System.currentTimeMillis();
  manager.notify(num, builder.build())

同时更改待定意向中的id

 pendingIntent = PendingIntent.getActivity(this, num, mainIntent, PendingIntent.FLAG_ONE_SHOT);

就我而言,我使用的是与您相同的id,只显示了最后一个通知。我的解决方案很简单,为notification_id生成一个随机数:

int id = (int)(Math.random() * 101);

相关内容

  • 没有找到相关文章

最新更新