我在使用时遇到了问题,因为我已经重写了handleIntent()方法,以便在应用程序处于前台时接收通知。我还使用了onMessageReceived()方法。
但是当应用程序处于后台时,我将收到两个通知,其中一个只打开应用程序并运行MainActivity,而另一个则以我想要的方式打开应用程序。
FYI:我在前台时收到的通知正是我希望它打开的方式。
以下是我编写的代码:
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();R
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) {
super.handleIntent(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 {
pendingIntent = PendingIntent.getActivity(this, 0, productIntent,
PendingIntent.FLAG_ONE_SHOT);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(intent.getExtras().getString("gcm.notification.title"))
.setContentText(intent.getExtras().getString("gcm.notification.body"))
.setDefaults(NotificationCompat.DEFAULT_VIBRATE)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentIntent(pendingIntent);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
}}
我已经从NotificationCompat.Builder方法中删除了onMessageReceived()。但我在后台仍收到两份通知。
App Gradle:
compile 'com.google.firebase:firebase-core:11.4.2' //Firebase
compile 'com.google.firebase:firebase-messaging:11.4.2' //Firebase Cloud Messaging我试过在网上搜索解决方案,但不幸的是,没有一个指向Android的解决方案。
发布于 2018-01-04 06:48:31
您正在将通知处理到handleIntent(Intent intent)中。您可能应该删除super.handleIntent(intent);,以防止Firebase系统在应用程序处于后台时处理通知。
解决方案:删除super.handleIntent(intent);
发布于 2018-01-04 06:57:35
只需创建sendnotification()方法,并设置您想要传递的任何参数,比如sendnotification(String )。使用挂起的意图启动您的活动,当您单击通知时,应用程序将数据解析为清单中定义的启动程序活动,因此一旦您在启动程序活动中有了数据,就可以使用意向将数据发送到其他活动。
我认为.setContentText("")被调用超过1次,您是否收到了两次相同的通知?
发布于 2018-01-04 06:58:50
是的,当你在后台应用时,你会收到推在系统托盘所以系统托盘将创建推通知标题和消息。当您单击推送时,您的初始启动程序活动(在清单中提到启动程序)将打开。
您可以在启动程序活动(bundle)中获得通知数据。
private void handlePush() {
Intent intent = null;
if (bundle.getString("push_type") != null && bundle.getString("push_type").length() > 0) {
switch (bundle.getString("push_type")) {
case PUSH_TYPE_FOLLOW_USER: {
intent = new Intent(this, ProfileExternalActivity.class);
intent.putExtra(Constants.USER_ID, Integer.parseInt(bundle.getString("id")));
intent.putExtra(Constants.FROM_PUSH_NOTIFICATION_SPLASH, true);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
break;
}
}
if (intent != null)
startActivity(intent);
finish();
}
}你需要检查活动是否有数据
if (bundle != null)
handlePush();
else //your next activity金融时报:https://firebase.google.com/docs/cloud-messaging/android/receive
或
您可以在通知中获取有效载荷对象而不是数据对象,如果通知对象中有有效负载对象,则将在您的通知对象中收到的所有时间推送。
https://stackoverflow.com/questions/48089965
复制相似问题