FCM通知onclick无法打开所需的活动 [英] FCM Notification onclick does not open desired activity

查看:94
本文介绍了FCM通知onclick无法打开所需的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过这里发布的几乎所有解决方案以及每个标志的组合,但是它不起作用.

i have tried almost every solution posted here and combination of every flag but it is not working.

以下是我遇到问题的用例.

Following are use cases in which i am having problems.

1)当我在应用程序 FCM 中时,通知会打开我想要的活动.数据在主要活动中传递给 onNewIntent .当应用程序是前台时,它可以正常工作.

1) When i am in application FCM notification opens my desired activity. Data is passed to onNewIntent in main activity. It works fine when application is foreground.

2)在后台模式(单击主页按钮)后,单击通知后,即使我在清单文件中将 android:launchMode ="singleTop" 指定为我的主要对象,它也会启动我的应用程序的新实例活动,但我不知道这里发生了什么.我想在此处打开的所需活动是 RateEventActivity

2) When in background mode(presssing home button) after clicking on notification it launches new instance of my application even though i have specified android:launchMode="singleTop" in manifest file to my main activity but i don't know what is happening here. Desired Activity i want to open here is RateEventActivity

这里发生的一件奇怪的事是,即使应用程序处于后台模式,但 NotificationActivity ProfileActivity 都可以正常工作/strong>产生了如上所述的所有问题.大多数应用程序代码是由我在 RateEevent 模块上工作的其他人编写的,所以我不知道我在这里缺少什么?

the strange thing happening here is that NotificationActivity and ProfileActivity are working perfectly even when application is in background mode but RateEventActivity is producing all the problem as i described above. Most of application code is written by someone else i am working on RateEevent module so i don't know what i am missing here?

意图在某处丢失(未在创建时传递给 mainActivty ).

Intents are lost somewhere (not passed to mainActivty on create) when i am clicking notification from background mode in RateEventActivity scenario.

我也尝试过将唯一的ID传递给我的待定意图,但是没有运气.我花了很多时间和精力来解决这个问题,但每次都失败了

i have also tried passing unique id to my pending intent but no luck. I have put lot of time and effort to solve this but failing every time

这是我的FCM代码

  private void createNotification(RemoteMessage remoteMessage) {
    RemoteMessage.Notification notification = remoteMessage.getNotification();
    NotificationManager mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);

    Log.v("Notification Title", remoteMessage.getNotification().getTitle());

    Intent intent = new Intent(this, MainActivity.class);
    //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    if (remoteMessage.getData().containsKey("notificationUser")) {
        intent.putExtra("notificationUser", remoteMessage.getData().get("notificationUser"));
    } else if (remoteMessage.getData().containsKey("notificationId")) {
        intent.putExtra("notificationId", remoteMessage.getData().get("notificationId"));
    } else if (remoteMessage.getData().containsKey("event")) {
        if (remoteMessage.getNotification().getTitle().equalsIgnoreCase("Event Feedback")) {


            Log.v("Notification Event", remoteMessage.getData().get("event"));
            intent.putExtra("eventId", remoteMessage.getData().get("event"));
        }


    }


    //PendingIntent.FLAG_UPDATE_CURRENT
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0 , intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_notification)
                    .setLargeIcon(largeIcon)
                    .setContentTitle(notification.getTitle())
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(notification.getBody()))
                    .setContentText(notification.getBody())
                    .setAutoCancel(true);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

主要活动代码 onCreate 方法

   if (getIntent().hasExtra("notificationId")) {
        Intent intent = new Intent(this, NotificationActivity.class);
        intent.putExtra("notificationId", getIntent().getStringExtra("notificationId"));
        startActivity(intent);
    } else if (getIntent().hasExtra("user")) {
        Intent intent = new Intent(this, ProfileActivity.class);
        intent.putExtra("userId", getIntent().getStringExtra("user"));
        startActivity(intent);
    } else if (getIntent().hasExtra("eventId")) {
        Intent intent = new Intent(this, RateEventActivity.class);
        intent.putExtra("eventId", getIntent().getStringExtra("eventId"));
        startActivity(intent);
    }

我的清单文件

 <activity
        android:name=".activities.MainActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:launchMode="singleTop"
        android:screenOrientation="portrait"
        android:theme="@style/HomeTheme" />

<activity
        android:name=".activities.ProfileActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="Profile"
        android:screenOrientation="portrait" />

 <activity
        android:name=".activities.NotificationActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="Notifications"
        android:screenOrientation="portrait" />

<activity
        android:name=".activities.RateEventActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="Rate Users"
        android:screenOrientation="portrait"
       />

推荐答案

如果要打开应用并执行特定操作(在后台运行),请在通知有效负载中设置click_action并将其映射到意图过滤器在您要启动的活动中.例如,将click_action设置为OPEN_ACTIVITY_1以触发意图过滤器,如下所示:

如react-native-fcm文档中所建议,要求后端以这种形式发送json数据,

As suggested in react-native-fcm docs,ask backend to send json data in the form like this,

 {

    "to":"some_device_token",

    "content_available": true, 
    "notification": {
    "title": "hello",
    "body": "yo",
    "click_action": "OPEN_ACTIVITY_1" // for intent filter in your activity
    },

"data": {
"extra":"juice"
}
}

并在您的mainfest文件中为您的活动添加意图过滤器,如下所示:

and in your mainfest file add intent-filter for your activity as below

<intent-filter>
  <action android:name="OPEN_ACTIVITY_1" />
  <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

单击通知时,它将打开应用程序并直接转到您在click_action中定义的活动,在本例中为"OPEN_ACTIVTY_1".在该活动中,您可以通过以下方式获取数据:

When you click the notification, it will open the app and go straight to activity that you define in click_action, in this case "OPEN_ACTIVTY_1". And inside that activity you can get the data by :

Bundle b = getIntent().getExtras();// add these lines of code to get data from notification
String someData = b.getString("someData");

查看以下链接以获取更多帮助:

check out below links for more helps:

Firebase FCM通知click_action负载

​​ Firebase onMessage在后台应用程序未接收到消息

Firebase控制台:如何为通知指定click_action

这篇关于FCM通知onclick无法打开所需的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆