简体   繁体   English

在后台单击通知时打开特定活动

[英]Open specific activity when notification clicked in background

There are two types of messages in FCM (Firebase Cloud Messaging): FCM(Firebase云消息传递)中有两种消息类型:

Display Messages: These messages trigger the onMessageReceived() callback only when your app is in foreground 显示消息:仅当您的应用程序处于前台时,这些消息才触发onMessageReceived()回调

Data Messages: Theses messages trigger the onMessageReceived() callback even if your app is in foreground/background/killed 数据消息:这些消息会触发onMessageReceived()回调,即使您的应用程序处于前台/后台/已终止状态

When the notification is received in foreground i am able to open the specific activity on notification press but when the notification is received in background i can t handle the notification press(it launches automatically the main activity) 当在前台收到通知时,我可以在通知媒体上打开特定的活动,但是在后台收到通知时,我无法处理通知媒体(它将自动启动主要活动)

I did some search and found : 我做了一些搜索,发现:

I added these intent-filter to my notification activity: 我在通知活动中添加了这些意图过滤器:

<activity android:name="com.mahdi.tiger.alahedclubnewtesting.activity.News_description"
            android:exported="true">
            <intent-filter>
                <action android:name="com.mahdi.tiger.alahedclubnewtesting.activity.News_description"></action>
                <category android:name="android.intent.category.DEFAULT"></category>
            </intent-filter>
        </activity>

but it doesn t work.. 但这不起作用..

I found also : 我还发现:

When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default.

So what should i do ? 所以我该怎么做 ? i want when the notification is received in background to handle the click and open my specific activity. 我想在后台收到通知时处理点击并打开我的特定活动。

This is the code i am using: 这是我正在使用的代码:

    public class myFirebaseMessagingService extends FirebaseMessagingService {

        private static final String TAG = "FirebaseMessageService";
        Bitmap bitmap;

        /**
         * Called when message is received.
         *
         * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
         */
        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            // There are two types of messages data messages and notification messages. Data messages are handled
            // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
            // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
            // is in the foreground. When the app is in the background an automatically generated notification is displayed.
            // When the user taps on the notification they are returned to the app. Messages containing both notification
            // and data payloads are treated as notification messages. The Firebase console always sends notification
            // messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options
            //
            Log.d(TAG, "From: " + remoteMessage.getFrom());

            // Check if message contains a data payload.
            if (remoteMessage.getData().size() > 0) {
                Log.d(TAG, "Message data payload: " + remoteMessage.getData());
            }

            // Check if message contains a notification payload.
            if (remoteMessage.getNotification() != null) {
                Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());

                String title=remoteMessage.getNotification().getTitle();
                String body=remoteMessage.getNotification().getBody();
                String action=remoteMessage.getNotification().getClickAction();



                sendNotification2(title,body,action);
            }

            //The message which i send will have keys named [message, image, AnotherActivity] and corresponding values.
            //You can change as per the requirement.
            String text = remoteMessage.getData().get("title");
            //message will contain the Push Message
            String message = remoteMessage.getData().get("message");
            //imageUri will contain URL of the image to be displayed with Notification
            //If the key AnotherActivity has  value as True then when the user taps on notification, in the app AnotherActivity will be opened.
            //If the key AnotherActivity has  value as False then when the user taps on notification, in the app MainActivity will be opened.
            String TrueOrFlase = remoteMessage.getData().get("AnotherActivity");

            //To get a Bitmap image from the URL received
            sendNotification(message, text,TrueOrFlase);

        }


        /**
         * Create and show a simple notification containing the received FCM message.
         */

        private void sendNotification(String messageBody,String text, String TrueOrFalse) {
            Intent intent = new Intent(this, Slider_description.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.putExtra("AnotherActivity", TrueOrFalse);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                    PendingIntent.FLAG_ONE_SHOT);

            Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.ahed_me)
                    .setContentTitle(text)
                    .setAutoCancel(true)
                    .setContentText(messageBody)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);

            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
        }
}

NOTE: i am using Firebase console to send notification. 注意:我正在使用Firebase控制台发送通知。

Thanks a lot. 非常感谢。

After a bit of testing I found this. 经过一些测试,我发现了这一点。

https://github.com/firebase/quickstart-android/blob/master/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/MyFirebaseMessagingService.java https://github.com/firebase/quickstart-android/blob/master/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/MyFirebaseMessagingService.java

The interesting part for you. 对您来说有趣的部分。

There are two types of messages data messages and notification messages. 消息有两种类型:数据消息和通知消息。 Data messages are handled here in onMessageReceived whether the app is in the foreground or background. 无论应用程序是在前台还是在后台,都在onMessageReceived中处理数据消息。 Data messages are the type traditionally used with GCM. 数据消息是GCM传统上使用的类型。 Notification messages are only received here in onMessageReceived when the app is in the foreground. 当应用程序在前台时,仅在onMessageReceived中在此处接收到通知消息。 When the app is in the background an automatically generated notification is displayed. 当应用程序在后台运行时,将显示自动生成的通知。 When the user taps on the notification they are returned to the app. 当用户点击通知时,他们将返回到应用程序。 Messages containing both notification and data payloads are treated as notification messages. 同时包含通知和数据有效负载的消息被视为通知消息。 The Firebase console always sends notificationmessages. Firebase控制台始终发送通知消息。

If you want full control on the notification you need to send data message. 如果要完全控制通知,则需要发送数据消息。 You can not use the firebase console to do it from my understanding. 根据我的理解,您不能使用firebase控制台执行此操作。

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

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