我需要PendingIntent根据用户收到的通知类型打开不同的活动



当一个用户收到另一个用户的消息时,他们会收到来自Firebase Cloud Messaging系统的通知。当用户点击该通知时,它会将他们带到MessageActivity,这很好。

我还有一些其他例子,用户会收到其中一个Firebase Cloud Messaging通知:当有人评论你的帖子、点赞你的帖子等时。当他们收到这些通知时,显然应该把它们带到PostActivity,而不是MessageActivity

那么,我应该写另一个"MyFirebaseInstanceServiceActivity"吗?还是我可以做一些简单的更改,这样如果是注释通知,它会把你带到PostActivity而不是MessageActivity

MyFirebaseInstanceService

public class MyFirebaseInstanceService extends FirebaseMessagingService {
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String sented = remoteMessage.getData().get("sented");
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
if (firebaseUser != null && sented.equals(firebaseUser.getUid())) {
sendNotification(remoteMessage);
}
}
private void sendNotification(RemoteMessage remoteMessage) {
String user = remoteMessage.getData().get("user");
String icon = remoteMessage.getData().get("icon");
String title = remoteMessage.getData().get("title");
String body = remoteMessage.getData().get("body");
RemoteMessage.Notification notification = remoteMessage.getNotification();
int j = Integer.parseInt(user.replaceAll("[\D]", ""));
Intent intent = new Intent(MyFirebaseInstanceService.this, MessageActivity.class);
Bundle bundle = new Bundle();
bundle.putString("id", user);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(MyFirebaseInstanceService.this, j, intent, PendingIntent.FLAG_ONE_SHOT);
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(MyFirebaseInstanceService.this, NOTIFICATION_CHANNEL_ID);
builder.setSmallIcon(R.drawable.ic_notification_events);
builder.setContentTitle(title);
builder.setContentText(body);
builder.setAutoCancel(true);
builder.setSound(sound);
builder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
notificationChannel.enableVibration(true);
notificationChannel.setLightColor(Color.BLUE);
notificationManager.createNotificationChannel(notificationChannel);
}
int i = 0;
if (j > 0)
i = j;
notificationManager.notify(i, builder.build());
}
@Override
public void onNewToken(@NonNull String s) {
super.onNewToken(s);
Log.d("TOKEN", s);
Task<InstanceIdResult> task = FirebaseInstanceId.getInstance().getInstanceId();
task.addOnSuccessListener(instanceIdResult -> {
if (task.isSuccessful()) {
String token = task.getResult().getToken();
sendRegistrationToServer(token);
Log.d("TOKEN", token);
}
});
task.addOnFailureListener(e -> {
if (!task.isSuccessful()) {
Exception exception = task.getException();
Log.d("TOKEN", exception.getMessage());
}
});
}
private void sendRegistrationToServer(String token) {
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
if (firebaseUser != null) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Tokens");
Token token1 = new Token(token);
reference.child(firebaseUser.getUid()).setValue(token1);
}
}
}

只需简单的检查就可以完成任务,您需要发送一些数据进行检查,以了解通知是否来自评论、点赞、帖子或其他

private void sendNotification(RemoteMessage remoteMessage) {
.....
String user = remoteMessage.getData().get("user");
String nType= remoteMessage.getData().get("type");// here type is some data you send
int j = Integer.parseInt(user.replaceAll("[\D]", ""));
...
Intent intent = null;
PendingIntent pendingIntent=null;
Bundle bundle = new Bundle();
if(nType.equals("message")){
intent=new Intent(MyFirebaseInstanceService.this, MessageActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);// here you can change launch mode if you want
bundle.putString("id", "124");// here add your data to the bundle
intent.putExtras(bundle);
pendingIntent = PendingIntent.getActivity(MyFirebaseInstanceService.this, j, intent, PendingIntent.FLAG_ONE_SHOT);
}else  if(nType.equals("comment")){
intent = new Intent(MyFirebaseInstanceService.this, PostActivity.class);
bundle.putString("postId", "134");// here add your data to the bundle
intent.putExtras(bundle);
// here You may need to user TaskStackBuilder  so if the user click back from PostActivity it goes to MessageActivity
TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(MyFirebaseInstanceService.this);
taskStackBuilder.addNextIntentWithParentStack(intent);
pendingIntent = taskStackBuilder
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
}
.....
}

这是如何使用TaskStackBuilder 的链接

最新更新