我正在使用Firebase云消息接收安卓通知。每个通知都应导航到不同的活动。我收到多个通知,但是当我单击多个通知中的任何一个时,它正在导航到正确的活动,但剩余的通知重定向到默认页面
这是我的代码
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
FirebaseFirestore db = FirebaseFirestore.getInstance();
FirebaseAuth mAuth;
String flag = "0";
String cid = "0",status11,opdat;
int ff;
String docid = "0";
Date d = new Date();
//deyaPAy
String docId;
String FirstName = "0";
String Amount = "0";
String Flag = "0";
String status = "0";
String SenderID1 = "0" ;
String ReceiverId1 = "0" ;
String ReceiverPhoneNumberst ="0";
String SenderAutoId ="0";
String SenderPhonenumber = "0";
String sendername = "0";
String receiverautoID ="0";
/**
* Called when message is received.
*
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
mAuth = FirebaseAuth.getInstance();
//user_id = mAuth.getUid();
// [START_EXCLUDE]
// There are two types of messages data messages and notification messages. Data messages are handled
// here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
// traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
// is in the foreground. When the app is in the background an automatically generated notification is displayed.
// When the user taps on the notification they are returned to the app. Messages containing both notification
// and data payloads are treated as notification messages. The Firebase console always sends notification
// messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options
// [END_EXCLUDE]
//
// Not getting messages here? See why this may be:
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getTitle());
String id = remoteMessage.getNotification().getTitle();
Log.e("id",id);
flag = remoteMessage.getData().get("code");
Log.e("qwert",flag);
cid = remoteMessage.getData().get("OrderId");
//deyaPay
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
Amount = remoteMessage.getData().get("Amount");
System.out.println(Amount);
SenderID1=remoteMessage.getData().get("SenderID");
System.out.println("Senderid"+SenderID1);
ReceiverId1=remoteMessage.getData().get("ReceiverId");
ReceiverPhoneNumberst=remoteMessage.getData().get("ReceiverPhoneNumber");
SenderPhonenumber = remoteMessage.getData().get("SenderPhoneNumber");
//long ReceiverPhoneNumber = Long.parseLong(ReceiverPhoneNumberst);
SenderAutoId=remoteMessage.getData().get("SenderAutoId");
status = remoteMessage.getData().get("Status");
sendername = remoteMessage.getData().get("SenderName");
receiverautoID = remoteMessage.getData().get("ReceiverAutoID");
System.out.println(Flag);
Log.d(TAG, "From:" + remoteMessage.getFrom());
Log.d(TAG, "Notification MessageBody:" + remoteMessage.getNotification().getBody());
Log.d(TAG, "Notification data:" + remoteMessage.getData());
}
if(Objects.equals(flag, "01") || Objects.equals(flag, "03") || Objects.equals(flag, "04") || Objects.equals(flag, "05")) {
final Map<String, Object> map = new HashMap<>();
map.put("Title", remoteMessage.getNotification().getTitle());
map.put("Body", remoteMessage.getNotification().getBody());
map.put("DateTime",d);
DocumentReference dr = db.collection("UserProfile").document(mAuth.getUid()).collection("Notifications").document();
docid = dr.getId();
dr.set(map);
}
else{
Log.e("","");
}
sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(),flag,cid,docid, sendername, Amount, SenderAutoId, status,SenderPhonenumber);
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
}
/**
* Create and show a simple notification containing the received FCM message.
*
* @param messageBody FCM message body received.
*/
private void sendNotification(String messageTitle, String messageBody,String flag1,String cid1,String docid1, String FirstName, final String amount, String senderautoid, String finalStatus,String SenderPhonenumber) {
System.out.println("messagebody"+messageBody);
System.out.println("Amount"+amount);
System.out.println("name"+FirstName);
System.out.println("senderid"+senderautoid);
String f1 = flag1;
Intent intent = null;
Date d = new Date();
String date = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(d);
String time = new SimpleDateFormat("HH:mm:ss a", Locale.getDefault()).format(d);
// write data to database base on falg values and method of delivery
if(Objects.equals(flag, "05")){
final Map<String, Object> map = new HashMap<>();
map.put("Id",cid1);
map.put("Type","Returned");
map.put("Date",date);
map.put("Time",time);
db.collection("UserProfile").document(mAuth.getUid()).collection("Notifications").document(docid1).set(map, SetOptions.merge());
intent = new Intent(MyFirebaseMessagingService.this, ReturnsClosedDetail.class);
intent.putExtra("ReturnId",cid);
}
else if(Objects.equals(flag, "11")){
intent = new Intent(MyFirebaseMessagingService.this, openorderdetails.class);
intent.putExtra("Orderid",cid);
}
else if(Objects.equals(flag, "12")){
intent = new Intent(MyFirebaseMessagingService.this, openorderdetails.class);
intent.putExtra("Orderid",cid);
}
else if(Objects.equals(flag, "13")){
intent = new Intent(MyFirebaseMessagingService.this, openorderdetails.class);
intent.putExtra("Orderid",cid);
}
else if(Objects.equals(flag, "07")){
intent = new Intent(MyFirebaseMessagingService.this, ReturnsOpenDetail.class);
intent.putExtra("ReturnId",cid);
}
else if(Objects.equals(flag, "14")){
intent = new Intent(MyFirebaseMessagingService.this, ReturnsOpenDetail.class);
intent.putExtra("ReturnId",cid);
}
else {
intent = new Intent(MyFirebaseMessagingService.this, ProfileNavigation.class);
}
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
// Notification icons
String channelId = "fcm_default_channel";
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.notifyicon)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((int) System.currentTimeMillis(), notificationBuilder.build());
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
}
//notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
尝试将PendingIntent
请求代码 0 替换为new Random().nextInt()
,如下所示
PendingIntent pendingIntent = PendingIntent.getActivity(this, new Random().nextInt(), intent,
PendingIntent.FLAG_ONE_SHOT);