第二次推送通知不会打开我的应用程序

时间:2018-01-26 08:00:55

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

我使用FCM Cloud Messaging服务,我可以从服务器一次生成一些通知。但唯一的问题是,我能够打开第一个通知,但是从第2个通知开始,直到剩下的通知在我点击它们时都没有做任何事情。

消息服务代码:

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());
    }
}

我怀疑它与" id"有关。变量我放入manager.notify()方法。

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

2 个答案:

答案 0 :(得分:0)

在我的情况下,我和你一样使用id,只显示了最后一个通知。我的解决方案很简单,为notification_id生成一个随机数:

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

答案 1 :(得分:0)

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

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

还要更改待处理意图中的id

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