简体   繁体   English

应用程序未运行时目标屏幕未打开 Firebase 通知 FCM?

[英]Target Screen not open when app is not running Firebase Notification FCM?

The problem is that When the App is running then notification is working fine (notification goes to targeted screen ) However tha app state is onPause or closwd then notification on click not open the targeted screen How i can solve it here is my code --->问题是当应用程序正在运行时,通知工作正常(通知转到目标屏幕)但是应用程序 state 处于 onPause 或 closwd 然后单击时通知无法打开目标屏幕我如何解决它这是我的代码 --- >

public class MyFirebaseMessagingService extends FirebaseMessagingService {

public static final String NOTIFICATION_CHANNEL_ID = "Sesame" ;
private  static final   String NOTIFICATION_NAME= "Sesame";
private static int count = 0;
private static final String TAG = MyFirebaseMessagingService.class.getName();

@Override
public void onMessageReceived(RemoteMessage message) {
    super.onMessageReceived(message);

    Map<String, String> data = message.getData();

    String title = data.get("title");
    String msg = data.get("message");
    String deepLink = data.get("deepLink");
    String slug = data.get("slug");

sendMyNotification(title,msg,deepLink,message);


}

private void sendMyNotification(String message, String body, String deeplink, RemoteMessage remoteMessage) {

 Intent intent = new Intent(this,MainActivity.class);
    if (remoteMessage.getData().size() >0){
        if (deeplink.equals("deepLink")){
            intent = new Intent(this, TermsConditionsActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            Log.i(TAG, "sendMyNotification: Terms ");
        }}


 intent.setPackage(null);
    intent.addFlags(Intent.FLAG_FROM_BACKGROUND | Intent.FLAG_RECEIVER_NO_ABORT);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT |
            PendingIntent.FLAG_ONE_SHOT |
            PendingIntent.FLAG_IMMUTABLE);

Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,NOTIFICATION_NAME, importance);
        mChannel.setDescription(message);
        mChannel.enableLights(true);
        mChannel.setLightColor(Color.RED);
        mChannel.enableVibration(true);
        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});

        mNotifyManager.createNotificationChannel(mChannel);
    }


NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, NOTIFICATION_NAME);
    mBuilder.setContentTitle(message)
            .setContentText(body)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setColor(Color.parseColor("#FFD600"))
            .setContentIntent(pendingIntent)
            .setChannelId(NOTIFICATION_CHANNEL_ID)
            .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
            .setPriority(NotificationCompat.PRIORITY_HIGH);

    mNotifyManager.notify(count, mBuilder.build());
    count++;}}

In my Manifest.xml file -->在我的 Manifest.xml 文件中 -->

    <service android:name=".notifications.MyFirebaseMessagingService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/colorAccent" />
    </service>

And Target Activity as ->目标活动为 ->

  <activity android:name=".activities.TermsConditionsActivity" >
        <intent-filter>
            <action android:name="deepLink"></action>
            <category android:name="android.intent.category.DEFAULT"></category>
        </intent-filter>
    </activity>

And in my Target Activity i subscribe the topic在我的目标活动中,我订阅了该主题

   FirebaseMessaging.getInstance().subscribeToTopic("topic");

Please help me..请帮我..

  1. Why you activity not getting open only when app is in BG:为什么仅当应用程序处于 BG 时您的活动才打开:

Reference: Firebase onMessageReceived not called when app in background参考: Firebase onMessageReceived app在后台不调用

This is working as intended, notification messages are delivered to your onMessageReceived callback only when your app is in the foreground.这是按预期工作的,仅当您的应用程序位于前台时,通知消息才会传递到您的 onMessageReceived 回调。 If your app is in the background or closed then a notification message is shown in the notification center, and any data from that message is passed to the intent that is launched as a result of the user tapping on the notification.如果您的应用程序处于后台或已关闭,则通知中心会显示一条通知消息,并且该消息中的任何数据都会传递给因用户点击通知而启动的 Intent。

You can specify a click_action to indicate the intent that should be launched when the notification is tapped by the user.您可以指定一个 click_action 来指示当用户点击通知时应启动的意图。 The main activity is used if no click_action is specified.如果未指定 click_action,则使用主要活动。

When the intent is launched you can use the启动意图后,您可以使用

getIntent().getExtras(); getIntent().getExtras(); to retrieve a Set that would include any data sent along with the notification message.检索一个 Set,其中包含随通知消息一起发送的任何数据。

For more on notification message see docs.有关通知消息的更多信息,请参阅文档。

2: How to get call in onMessageReceived if app in BG 2:如果应用程序在BG中,如何在onMessageReceived中调用

Refrence: Firebase onMessageReceived not called .参考: Firebase onMessageReceived 未调用 (read this complete answer) (阅读这个完整的答案)

Your messages, that are sent from somewhere, need to have a data payload and no notification payload, or they won't arrive consistently inside of "onMessageReceived" in your MessagingService.从某处发送的消息需要有数据有效负载而没有通知有效负载,否则它们将不会始终如一地到达 MessagingService 中的“onMessageReceived”。

暂无
暂无

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

相关问题 Firebase Web 应用程序中的通知收件箱(非 FCM) - Firebase Notification Inbox in Web App (Not FCM) 应用程序被杀死时 FCM 通知不会触发 - FCM Notification not firing when app is killed Firebase (FCM):打开活动并在通知点击时传递数据。 android - Firebase (FCM): open activity and pass data on notification click. android 如何使用 Ionic 向我的应用程序的所有用户推送带有 firebase FCM 的通知? - How to push notification with firebase FCM to all users of my App with Ionic? Flutter Firebase 消息 - 应用打开时不显示推送通知 - Flutter Firebase messaging - push notification is not showing when app is open 如何使用 react-native-firebase/messaging(FCM) 从栏点击通知后将应用程序打开到特定页面? - How to open app to specific page after click notification from bar by using react-native-firebase/messaging(FCM)? 在 Kotlin 客户端应用程序中发送 FCM 推送通知 - Firebase 云消息传递 - Sending FCM Push Notification in Kotlin Client App - Firebase Cloud Messaging 后台通知 OnClick With FCM Flutter app open an empty activity - Background Notification OnClick With FCM Flutter app open up an empty activity FCM - Flutter 点击通知打开应用程序(所有州) - FCM - Flutter tap on notification to open app (all states) 当我单击 FCM(Firebase 云消息传递)通知时,应用程序重新启动 - When I click on a FCM (Firebase Cloud Messaging) notification, the application restarts
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM