当应用程序在后台运行时,为什么onMessageReceived()方法未正确传递意图? [英] Why onMessageReceived() method is not passing intent properly when the app is in background?

查看:193
本文介绍了当应用程序在后台运行时,为什么onMessageReceived()方法未正确传递意图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,我试图在点击push-notification时将意图传递给activity,并且当foreground中存在该应用程序时,它可以正常工作.但是当该应用程序在后台运行时,尽管我收到了push-notification没问题,但是通过单击该通知,我并没有意图进入activity.我试图在SO中引用一些问题,并尝试添加诸如PendingIntent.FLAG_UPDATE_CURRENT之类的标志, PendingIntent.FLAG_ONE_SHOT,然后按意图(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP )进行操作,但没有一个是有帮助的.请帮助我解决这个问题,从最近两天开始脖子一直很疼.

Actually I'm trying to pass intent to an activity on click of the push-notification and it's working fine when the app is present in foreground.But when the app is in background although I'm receiving the push-notification without any problem but by clicking that notification I'm not getting intent in the activity. I tried to refer some questions in SO and tried adding flags likePendingIntent.FLAG_UPDATE_CURRENT, PendingIntent.FLAG_ONE_SHOT,then in intent (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP ) but none of them were helpful . Please help me to solve this issue it's been a pain in the neck from last two days.

Manifest.XML:

<activity android:name=".activities.ProfileHolder"
        android:windowSoftInputMode="adjustPan"
        android:screenOrientation="portrait"
        android:exported="true"
        android:launchMode="singleTop"
       >
        <intent-filter>
            <action android:name="profile" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

Node.js中的通知代码:

var message = {
         to: u.fcm_token, 
         collapse_key: 'white', //anything can be given
         data: {
             type:"likes",
             post_owner_username:data.like_post_owner_username,
             owner_post_time:data.like_post_owner_time

         },
         priority: 'high',
         notification: {
             title: 'Infinity',
             body:notif_data,
             sound: "default",
             click_action: "profile",
             tag:"Hey Bro" //if specified then current msg would be  replaced by new msg
         }
     };

     fcm.send(message, function(err, response){
         if (err) {
             console.log(err);
         } else {
             console.log("Successfully sent with response: ", response);
         }
     });

FirebaseMessagingService类:

/Varibales for notifications
String title, body, post_owner_username, post_owner_time, notif_commenter_username, notif_comment_time, follower_username,messager_username,type,action;
@Override
public void onMessageReceived(final RemoteMessage remoteMessage) {
    title=remoteMessage.getNotification().getTitle();
    body=remoteMessage.getNotification().getBody();
    type=remoteMessage.getData().get("type");
    action=remoteMessage.getNotification().getClickAction();

    Log.d("type",type);
    Log.d("type-2",title);

   if(type.equals("likes")){
       post_owner_username=remoteMessage.getData().get("post_owner_username");
       post_owner_time=remoteMessage.getData().get("owner_post_time");
   }
   else if(type.equals("comments")||type.equals("comment_likes")){
       post_owner_username=remoteMessage.getData().get("post_owner_username");
       post_owner_time=remoteMessage.getData().get("owner_post_time");
       notif_commenter_username=remoteMessage.getData().get("commenter_username");
       notif_comment_time=remoteMessage.getData().get("comment_time");
   }
   else if(type.equals("follow")){
       follower_username=remoteMessage.getData().get("follower_username");
   }
   else if(type.equals("messaging")){
       messager_username=remoteMessage.getData().get("sender_username");
   }

    //Showing Notification

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(getApplicationContext(), Constants.CHANNEL_ID)
                    .setSmallIcon(R.drawable.new_icon)
                    .setContentTitle(title)
                    .setContentText(body);

    Intent i = new Intent(action);
    Bundle b = new Bundle();
    if (type.equals("likes")) {
        b.putString("post_owner_username", post_owner_username);
        b.putString("post_owner_time", post_owner_time);
        b.putString("what", "show_notification_post");
        b.putString("Open","starred");

        // i.putExtra("Open", "starred");
    }
    else if (type.equals("comments")) {
        b.putString("post_owner_username",post_owner_username);
        b.putString("post_owner_time", post_owner_time);
        b.putString("notif_commenter_username", notif_commenter_username);
        b.putString("notif_comment_time",notif_comment_time);
        b.putString("what", "show_notification_post");
        b.putString("Open","starred");
        //i.putExtra("Open", "starred");
    }
    else if (type.equals("comment_likes")) {

        b.putString("post_owner_username", post_owner_username);
        b.putString("post_owner_time", post_owner_time);
        b.putString("notif_commenter_username", notif_commenter_username);
        b.putString("notif_comment_time", notif_comment_time);
        b.putString("what", "show_notification_post");
        b.putString("Open","starred");
        // i.putExtra("Open", "starred");
    }
    else if (type.equals("follow")||type.equals("follow_request")) {
        b.putString("searchUsername", follower_username);
        b.putString("Open","search_profile");
        //i.putExtra("Open", "search_profile");
    }
    else if(type.equals("messaging")){
        b.putString("Open","messaging");
        b.putString("msg_username",messager_username);
    }

    i.putExtras(b);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP );
    //ctx.startActivity(i);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, i, PendingIntent.FLAG_UPDATE_CURRENT);

    /*
     *  Setting the pending intent to notification builder
     * */

    mBuilder.setContentIntent(pendingIntent);

    NotificationManager mNotifyMgr =
            (NotificationManager) getSystemService(NOTIFICATION_SERVICE);


    if (mNotifyMgr != null) {
        mBuilder.setAutoCancel(true);
        mNotifyMgr.notify(1, mBuilder.build());
    }
}

推荐答案

我认为问题是因为您正在从fcm控制台发送通知!

i think the problem is because your are sending notification from fcm console !

来自文档

"当您的应用位于用户设备的后台时,通知 交付给系统托盘.当用户点击 通知,应用启动器会打开您的应用.如果您愿意,可以 还添加客户端消息处理以在您的应用程序中接收通知 当它已经在用户设备的前台时."

"When your app is in the background on a user's device, notifications are delivered to the system tray. When a user clicks on the notification, the app launcher opens your app. If you want, you can also add client message handling to receive notifications in your app when it is already in the foreground on the user's device."

您应该尝试使用Fcm api发送通知

you should try sending notification with Fcm apis

假设 我认为您在使用api时不应该发送通知参数,只是发送数据参数并解析onMessageReceived中的数据(如果您发送通知,它将被发送到系统托盘

Hypothesis i think you should not send notification parameter when you are using api just send data parameter and parse the data in your onMessageReceived if you send notification it will delivered to system tray

如何在后台运行应用程序时处理通知Firebase

这篇关于当应用程序在后台运行时,为什么onMessageReceived()方法未正确传递意图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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