代码之家  ›  专栏  ›  技术社区  ›  Laxman parlapelly

FCM点击如何在应用程序处于后台状态时打开用户指定的活动而不是默认活动

  •  0
  • Laxman parlapelly  · 技术社区  · 6 年前

    FCM工作正常,当app处于前台状态时,通知出现在设备上,当点击通知时,它将重定向到我指定的活动,因此它工作正常。

    这是 MyFirebaseMessagingService公司 班级:

    public class MyFirebaseMessagingService extends FirebaseMessagingService {
        private static final String TAG = "MyFirebaseMsgService";
        private String title, messageBody;
    
        /**
         * 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) {
            // [START_EXCLUDE]
    
            if (remoteMessage.getData().size() > 0) {
                Log.d(TAG, "Message data payload: " + remoteMessage.getData());
                if (remoteMessage.getData() != null && remoteMessage.getData().size() > 0) {
                    title = remoteMessage.getData().get("title");
                    if (TextUtils.isEmpty(title)) title = "Bocawest";
                    messageBody = remoteMessage.getData().get("message");
                }
                handleNow();
            }
    
            // Check if message contains a notification payload.
            if (remoteMessage.getNotification() != null) {
                Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    
                sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
            }
    
            // 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.
            if (!TextUtils.isEmpty(messageBody))
                sendNotification(title, messageBody);
            //sendNotification(remoteMessage.getNotification().getBody());
            Intent intent = new Intent();
            intent.setAction("com.android.bocawest");
            sendBroadcast(intent);
        }
        // [END receive_message]
    
        /**
         * Handle time allotted to BroadcastReceivers.
         */
        private void handleNow() {
            Log.d(TAG, "Short lived task is done.");
        }
    
        /**
         * Create and show a simple notification containing the received FCM message.
         *
         * @param messageBody FCM message body received.
         */
        private void sendNotification(String title, String messageBody) {
            PendingIntent pendingIntent;
            if (SharedPreference.getBoolean(getApplicationContext(), getApplicationContext().getResources().getString(R.string.sp_isLoginIN))) {
                Intent intent = new Intent(this, NotificationsActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                        PendingIntent.FLAG_UPDATE_CURRENT);
            } else {
                Intent intent = new Intent(this, LoginActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                        PendingIntent.FLAG_UPDATE_CURRENT);
            }
    
    
            String channelId = getString(R.string.default_notification_channel_id);
            Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder notificationBuilder =
                    new NotificationCompat.Builder(this, channelId)
                            .setSmallIcon(R.drawable.ic_launcher)
                            .setContentTitle(title)
                            .setContentText(messageBody)
                            .setAutoCancel(true)
                            .setSound(defaultSoundUri)
                            .setContentIntent(pendingIntent);
    
            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
            // Since android Oreo notification channel is needed.
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel(channelId,
                        "Bocawest",
                        NotificationManager.IMPORTANCE_DEFAULT);
                notificationManager.createNotificationChannel(channel);
            }
    
            notificationManager.notify(0, notificationBuilder.build());
        }
    }
    

    注: 通知活动 是我指定的活动。 家庭活动 是默认活动

    我知道有很多类似的问题,但是我还没有找到任何与我的用例相关的问题。 请帮帮我。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Jaykishan Sewak    6 年前

    @Laxman parlapelly根据Firebase standered 当你的应用在后台收到通知,用户点击通知后,它将只打开默认活动。

    例如,在您的案例中,当用户点击通知时,它将打开您的Home活动,而在Home活动的oncreate方法中,您需要打开NotificationsActivity(以及需要的bundle)


    什么时候?

    当应用程序在后台时点击通知 创建() 将调用HomeActivity的方法,以便您可以编写代码以打开通知活动

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);
            animLay = findViewById(R.id.root_lay_la);
            Intent intent = new Intent(this,NotificationActivity.class);
            //intent.putExtra("KEY",getIntent().getStringExtra("data")); if u need to pass data
            startActivity(intent);
        }
    
        2
  •  0
  •   Debu    6 年前

    if(SharedPreference.getBoolean(getApplicationContext(),getApplicationContext().getResources().getString(R.string.sp_islogin))) 家庭活动 通知活动 否则将继续 家庭活动

    支票- Navigate to different activities on notification click

        3
  •  0
  •   Ahmed Abdalla    6 年前

    这对我有用 onMessageReceived()

    Intent intent = new Intent(this, NotificationsActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_CLEAR_TASK);
    
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "111")
    .setSmallIcon(R.drawable.logo)
    .setContentTitle(getString(R.string.yhnn))
    .setContentText(title)
    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
    // Set the intent that will fire when the user taps the notification
    .setContentIntent(pendingIntent)
    .setSound(sound)
    .setAutoCancel(true);
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    
    // notificationId is a unique int for each notification that you must define
    notificationManager.notify(5, builder.build());