java - 我需要 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的链接
推荐阅读
- javascript - 过滤具有可选值的对象数组
- angular - 如何在 Angular(使用 ngx-owl-carousel-o)中添加动画(如淡入和淡出)?
- permissions - 无法运行 byobu,因为 [root] 不拥有 [/home/ubuntu]
- assembly - 从用户模式获取 x86 SMEP CPU 标志
- spring-boot - 在 Spring 单元测试中无法将 offsetDateTime 转换为 ISO-8601 字符串格式
- python - 无法找到 Xpath 元素
- php - 从 Woocommerce 3rd 方插件更改文本
- java - 结果映射集合不包含值错误,而 ResultMap.xml 到 @Mapper 类
- amazon-web-services - 有没有关于 aws-iot 设备影子的应用程序?
- javascript - 仅当元素可见时才执行单击功能