我有firebase推送通知的问题 . 它不会显示通知,而是自动打开我放置的活动 . 我想要这种行为,但点击通知后 .
这是我的代码:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
final String title = remoteMessage.getData().get("title");
final String message = remoteMessage.getData().get("body");
showNotifications(title, message);
}
private void showNotifications(String title, String msg) {
Intent i = new Intent(this, OrderDetailActivity.class);
i.putExtra("order_id", 99);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
i, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT);
Notification notification = new NotificationCompat.Builder(this)
.setContentText(msg)
.setContentTitle(title)
.setContentIntent(pendingIntent)
.setSmallIcon(R.mipmap.ic_launcher_logo)
.build();
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID, notification);
try {
pendingIntent.send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
}
1 回答
快看,我想知道为什么你需要
pendingIntent.send();
?来自send()上的文档:这就是为什么它可能正在执行附加的
Intent
. 尝试删除该行 . 有关更多信息,请参阅Create a Notification .