简体   繁体   English

FCM通知onclick无法打开所需的活动

[英]FCM Notification onclick does not open desired activity

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) When i am in application FCM notification opens my desired activity. 1)当我在应用程序中时, FCM通知会打开我想要的活动。 Data is passed to onNewIntent in main activity. 数据传递到主要活动中的onNewIntent It works fine when application is foreground. 当应用程序是前台时,它工作正常。

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. 2)当在后台模式(按下主页按钮)上单击通知后,即使我在清单文件android:launchMode="singleTop"指定为我的主要活动,它也会启动我的应用程序的新实例,但我不知道发生了什么这里。 Desired Activity i want to open here is RateEventActivity 我想在这里打开的所需活动是RateEventActivity

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. 这里发生的奇怪的事情是,即使应用程序处于后台模式, NotificationActivityProfileActivity可以正常工作,但是RateEventActivity会产生如上所述的所有问题。 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? 大多数应用程序代码是由我在RateEevent模块上工作的其他人编写的,所以我不知道我在这里缺少什么?

Intents are lost somewhere (not passed to mainActivty on create) when i am clicking notification from background mode in RateEventActivity scenario. 当我在RateEventActivity场景中从后台模式单击通知时,意图丢失在某处(在创建时未传递给mainActivty )。

i have also tried passing unique id to my pending intent but no luck. 我还尝试将唯一的ID传递给我的待定意图,但没有运气。 I have put lot of time and effort to solve this but failing every time 我花了很多时间和精力来解决这个问题,但每次都失败

here is my FCM code 这是我的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());
}

Main Activity Code onCreate Method 主要活动代码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);
    }

My Manifest File 我的清单文件

 <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"
       />

If you want to open your app and perform a specific action [while backgrounded], set click_action in the notification payload and map it to an intent filter in the Activity you want to launch. 如果要打开应用程序并执行特定操作(在后台运行),请在通知有效负载中设置click_action并将其映射到要启动的活动中的意图过滤器。 For example, set click_action to OPEN_ACTIVITY_1 to trigger an intent filter like the following: 例如,将click_action设置为OPEN_ACTIVITY_1以触发意图过滤器,如下所示:

As suggested in react-native-fcm docs,ask backend to send json data in the form like this, 正如react-native-fcm文档中建议的那样,请后端以这种形式发送json数据,

 {

    "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"
}
}

and in your mainfest file add intent-filter for your activity as below 并在您的mainfest文件中为您的活动添加意图过滤器,如下所示

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

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". 当您单击通知时,它将打开应用程序并直接进入您在click_action中定义的活动,在本例中为“ 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 notifications click_action payload Firebase FCM通知click_action有效负载

Firebase onMessageReceived not called when app in background 在后台运行应用程序时未调用Firebase onMessageReceived

Firebase console: How to specify click_action for notifications Firebase控制台:如何为通知指定click_action

i got this issue long ago. 我很早以前就收到了这个问题。 let me check my project code. 让我检查我的项目代码。

Ahh its here. 啊,在这里。

Try with setting flags 尝试设置标志

Intent intent = new Intent(this,    MainActivity.class));       
// set intent so it does not start a new activity
         intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
         Intent.FLAG_ACTIVITY_SINGLE_TOP);

and change your PendingIntent like below 并如下更改您的PendingIntent

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

Also remove 同时删除

android:launchMode="singleTop"

from your MainActivity from Manifest file 来自清单文件中的MainActivity

try this and let see it works. 试试看,让它起作用。

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

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