我正在为我的Android应用程序通知使用Firebase cloudmessaging,所以我的问题是,如果用户取消了该通知,我将发送通知,如果我单击后发送的下一个通知正在打开第一个被取消的通知,即使如果我发送了第三个通知,而用户同时关闭了第一个和第二个通知,则如果单击第三个通知,它将打开第一个通知。我正在将Firebase云消息传递与数据一起使用,并发送(标题,摘录,图像,链接)。在通知栏中,所有内容都很酷并且正确无误,但是当单击链接时,链接更改并且Webview将打开第一个通知。
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
sendNotification(remoteMessage.getData().get("title"), remoteMessage.getData().get("body"),
Integer.parseInt(remoteMessage.getData().get("topic")), remoteMessage.getData().get("link"), remoteMessage.getData().get("imageUrl"), Integer.parseInt(remoteMessage.getData().get("id")));
if (/* Check if data needs to be processed by long running job */ true) {
// For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
scheduleJob();
} else {
// Handle message within 10 seconds
handleNow();
}
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(),
0, " ", " ", 0);
}
}
// [END receive_message]
/**
* Schedule a job using FirebaseJobDispatcher.
*/
private void scheduleJob() {
// [START dispatch_job]
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
Job myJob = dispatcher.newJobBuilder()
.setService(MyJobService.class)
.setTag("my-job-tag")
.build();
dispatcher.schedule(myJob);
// [END dispatch_job]
}
/**
* Handle time allotted to BroadcastReceivers.
*/
private void handleNow() {
Log.d(TAG, "Short lived task is done.");
}
/**
* Create and show a simple notification containing the received FCM message.
*
* @param messageBody FCM message body received.
*/
@TargetApi(Build.VERSION_CODES.O)
private void sendNotification(String messageTitle, String messageBody, int topic, String link, String imageUrl, int id) {
PendingIntent pendingIntent;
if (topic == 1){
Intent intent = new Intent(this, WebActivity.class);
// Create the TaskStackBuilder and add the intent, which inflates the back stack
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(intent);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("link", link);
intent.putExtra("title", messageTitle);
// Get the PendingIntent containing the entire back stack
pendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_ONE_SHOT);
}else{
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("link", link);
intent.putExtra("topic", topic);
pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
}
String channelId = getString(R.string.default_notification_channel_id);
InputStream in;
Bitmap myBitmap = null;
try {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
in = connection.getInputStream();
myBitmap = BitmapFactory.decodeStream(in);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setPriority(NotificationManager.IMPORTANCE_DEFAULT)
.setChannelId(channelId)
.setSmallIcon(R.drawable.ic_stat_name)
.setLargeIcon(myBitmap)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorAccent))
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle().bigText(messageTitle))
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(myBitmap))
.setGroupSummary(true)
.setGroup(String.valueOf(topic))
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = "The Channel";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(channelId, name, importance);
channel.setDescription(description);
channel.setShowBadge(true);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
notificationManager.createNotificationChannelGroup(new NotificationChannelGroup(String.valueOf(topic), "Articles"));
}
notificationManager.notify(id /* ID of notification */, notificationBuilder.build());
}
}
预期的结果是,如果用户关闭了第一个通知,则单击该按钮,Webview将打开从通知发送的第二个信息。
答案 0 :(得分:0)
因此,经过大量研究,我发现我必须用PendingIntent.FLAG_UPDATE_CURRENT
更新我的意图,并在每次创建新意图时更改该意图的request code
,这就是任何一个人的新代码将来有这个问题:
PendingIntent pendingIntent;
if (topic == 1){
Intent intent = new Intent(this, WebActivity.class);
// Create the TaskStackBuilder and add the intent, which inflates the back stack
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(intent);
intent.putExtra("link", link);
intent.putExtra("title", messageTitle);
intent.setAction("actionstring" + System.currentTimeMillis());
// Get the PendingIntent containing the entire back stack
pendingIntent =
stackBuilder.getPendingIntent(id, PendingIntent.FLAG_UPDATE_CURRENT);
}else{
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("link", link);
intent.putExtra("topic", topic);
pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
}