繁体   English   中英

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

[英]I need for the PendingIntent to open up different Activities depending on the type of notification that the user receives

当一个用户收到来自另一个用户的消息时,他们会收到来自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的链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM