当一个用户收到另一个用户的消息时,他们会收到另一个用户的通知 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);
}
}
}
1条答案
按热度按时间knpiaxh11#
只需一个简单的检查就可以完成这项工作,你需要发送一些数据,你检查,以知道是否通知来自评论或喜欢或张贴或任何东西
下面是一个链接,指向如何使用taskstackbuilder