当前位置: 首页 > 知识库问答 >
问题:

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

景明诚
2023-03-14
<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>

我还发现:

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.
    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());
        }
}

共有1个答案

吴哲
2023-03-14

经过一些测试,我发现了这个。

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

对你来说有趣的部分。

 类似资料:
  • 我正在工作的应用程序中,我被要求显示通知。对于通知,我使用的是FireBase云消息(FCM)。我能够得到通知,当应用程序在后台。 2.)MyFirebaseInstanceIDService 这是我在MyFirebaseMessagingService类中为onMessageReceived()方法编写的代码示例。

  • 我有一个带有通知的应用程序,如果我单击它们,就会打开某个活动。我想要的是,如果我点击通知,活动已经打开,它不是重新开始,但只是出现在前面。 我可以用标志来管理它吗?还是我应该在SharedPreferences中存储一个变量来检查它是否已打开? 谢谢!

  • 我已经尝试了所有的方法,但它不适合我。我想打开或恢复应用程序,无论屏幕打开,同时单击通知。 我使用了以下方法:

  • 我知道这个问题似乎重复,但为了我的要求,我已经在网上搜索了很多帖子,但没有一个对我有效。 我的要求 为此,我尝试了以下场景 情景1 使用将通知数据从传输到Launcher/Main活动,并根据bundle数据重定向到特定活动/片段 情景3 通过使用 按照上面的场景,当应用程序打开时,一切工作都很好,但如果我的应用程序关闭了,则意味着它总是重定向到main/launcher活动

  • 仅当打开应用并执行通知单击时,通知单击才会启动指定的活动。如果应用程序处于后台/未运行,并且执行了通知单击,则会打开应用程序的 MainActivity。简而言之,这就像应用程序在活动堆栈之后正常打开,而不是在 PendingIntent 中打开指定的活动。 我想根据它们的类型将通知单击重定向到两个不同的活动(批准详细活动和对话详细活动)。 我使用FCM进行推送通知。我在这里粘贴我的Manifes

  • 我看到了一个Firebase控制台的代码:如何为通知指定click_action,我使用了该通知,但在初始化变量CLS时出现了一个错误。我试图通过定义CLS=NULL来解析,以清除错误。它无法使用click_action打开我指定的活动 拜托,我有没有搞错什么?我怎么才能让它起作用?