第二次推送通知没有打开我的应用程序
2nd Push Notification doesn't open my app
我正在使用 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());
}
}
我怀疑它与我在 manager.notify() 方法中输入的 "id" 变量有关。
有办法解决这个问题吗?
就我而言,我使用的是与您相同的 id
,并且只显示了最后一条通知。我的解决方案很简单,为 notification_id:
生成一个随机数
int id = (int)(Math.random() * 101);
是的,更改 id
,因为它覆盖了之前的通知,因此不会打开任何内容,将其更改为:
int num = (int) System.currentTimeMillis();
manager.notify(num, builder.build())
同时更改待处理意图中的 id
:
pendingIntent = PendingIntent.getActivity(this, num, mainIntent, PendingIntent.FLAG_ONE_SHOT);
我正在使用 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());
}
}
我怀疑它与我在 manager.notify() 方法中输入的 "id" 变量有关。
有办法解决这个问题吗?
就我而言,我使用的是与您相同的 id
,并且只显示了最后一条通知。我的解决方案很简单,为 notification_id:
int id = (int)(Math.random() * 101);
是的,更改 id
,因为它覆盖了之前的通知,因此不会打开任何内容,将其更改为:
int num = (int) System.currentTimeMillis();
manager.notify(num, builder.build())
同时更改待处理意图中的 id
:
pendingIntent = PendingIntent.getActivity(this, num, mainIntent, PendingIntent.FLAG_ONE_SHOT);