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

Firebase消息在应用程序处于前台时收到推送通知后停止

易扬
2023-03-14

所以我在firebase消息传递中经历了一个奇怪的行为。我在我的firebase messaging FCM调用中同时发送[通知]和[数据]对象,当应用程序在后台时,我可以接收到我调用FCM请求的多个推送通知。但现在的问题是,当我在打开应用程序时收到推送通知时,如果我点击了该通知,我将不再收到任何进一步的推送通知。

注意:如果我强行停止然后重新打开,我会再次开始收到通知。

我在几个安卓版本的安卓设备上测试了这个问题,得到了相同的结果。

顺便说一句,我正在使用compile'com.google.firebase:firebase-messaging:11.4.0'

更新:这里是代码

public class GcmMessageHandler extends FirebaseMessagingService {
    public static final int MESSAGE_NOTIFICATION_ID = 435345;
    String title, Type, Link, AppVersion, discountExpiry, discountValue, status,
            phone, nid;
    PreferencesHelper ph;
    Map data;

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        if (Looper.myLooper() == null) {
            Looper.prepare();
        }

        ph = new PreferencesHelper(getApplicationContext());

        data = remoteMessage.getData();
        String click_action = remoteMessage.getNotification().getClickAction();
        Intent intentNotifi = new Intent(click_action);
        intentNotifi.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        title = (String) data.get("title");
        Type = (String) data.get("type");
        Link = (String) data.get("link");
        discountExpiry = (String) data.get("discountexpiry");
        discountValue = (String) data.get("discvalue");
        AppVersion = (String) data.get("appversion");
        status = (String) data.get("status");
        phone = (String) data.get("delivery_phone");
        nid = (String) data.get("nid");

        if (Type != null) {
            Intent notificationIntent;
            switch (Type) {
                case "simple_notification":
                    notificationIntent = new Intent(getApplicationContext(), SplashActivity.class);
                    show_notification(title, notificationIntent);
                    break;

                case "order_status":
                    notificationIntent = new Intent(getApplicationContext(), OrderActivity.class);
                    notificationIntent.putExtra("fromNotification", true);
                    show_notification(title, notificationIntent);
                    break;

                case "news":
                    notificationIntent = new Intent(getApplicationContext(), DetailsFromNotification.class);
                    notificationIntent.putExtra("nidRef", nid);
                    notificationIntent.getBooleanExtra("fromNotification", true);
                    show_notification(title, notificationIntent);
                    break;

                case "link":
                    notificationIntent = new Intent(Intent.ACTION_VIEW);
                    notificationIntent.setData(Uri.parse(Link));
                    show_notification(title, notificationIntent);
                    break;

                case "deal_notify":
                    notificationIntent = new Intent(getApplicationContext(), DealsActivity.class);
                    notificationIntent.putExtra("fromNotification", true);
                    show_notification(title, notificationIntent);
                    break;

                case "after_ordering":
                    notificationIntent = new Intent(getApplicationContext(), OrderActivity.class);
                    notificationIntent.putExtra("fromNotification", true);
                    show_notification(title, notificationIntent);
                    break;

                case "no_orders":
                    notificationIntent = new Intent(getApplicationContext(), SplashActivity.class);
                    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    show_notification(title, notificationIntent);
                    break;

                case "no_signup":
                    notificationIntent = new Intent(getApplicationContext(), UserActivity.class);
                    notificationIntent.putExtra("fromNotification", true);
                    show_notification(title, notificationIntent);
                    break;

                case "user_points":
                    notificationIntent = new Intent(getApplicationContext(), PointsActivity.class);
                    notificationIntent.putExtra("fromNotification", true);
                    show_notification(title, notificationIntent);
                    break;
                default:
                    notificationIntent = new Intent(getApplicationContext(), SplashActivity.class);
                    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    show_notification(title, notificationIntent);
                    break;
            }
        }

        Looper.loop();
    }


    private void show_notification(String title, Intent intent) {
        Context context = getBaseContext();
        PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(getNotificationIcon()).setContentText(title).
                        setContentIntent(contentIntent)
                .setAutoCancel(true).setDefaults(Notification.DEFAULT_SOUND |
                        Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
        NotificationManager mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(MESSAGE_NOTIFICATION_ID, mBuilder.build());
    }

    private int getNotificationIcon() {
        boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >=
                android.os.Build.VERSION_CODES.LOLLIPOP);
        return useWhiteIcon ? R.drawable.logo_notification :
                R.drawable.logo_notification;
    }
} 

共有1个答案

钦枫
2023-03-14

我找到了问题的起因。出于某种原因

 if (Looper.myLooper() == null) {
            Looper.prepare();
        }

而且

Looper.loop();

不应该在代码中,并且正在阻止再次调用onMessageReceived。

 类似资料:
  • 当应用程序在前台时不接收推送通知,但当应用程序在后台时接收我已遵循来自https://medium . com/@ ankushaggarwal/GCM-setup-for-Android-push-notifications-656 cf DD 8 adbd的FCM教程

  • 我已经集成了Firebase,用于在flutter中推送通知。如果应用程序在前台,我已经显示了一个通知详细信息的对话框。我收到通知正确的Android。同样在iOS通知在后台和设备锁定时工作正常。我还启用了推送通知和后台抓取。对此有什么解决办法吗?有人面临过这样的问题吗? 我使用这个Firebase插件https://pub.dev/packages/firebase_messaging

  • 当应用程序在后台时,如何更新包含数据负载的Firebase推送通知?有没有办法在通知中指定通知id给Firebase API? 完整的项目在github https://github.com/akshatashan/FireBaseCloudMessagingDemo中

  • 我使用FCM(Firebase Cloud Messaging)在iOS中发送推送通知。 我能够在应用程序处于前台状态时收到通知。但当应用程序处于后台状态时,不会收到通知。每当应用程序进入前台状态时,才会收到通知。 我的代码是: 当App处于后台状态时: --根本不叫。 我在应用程序功能中启用了推送通知和后台模式下的远程通知。但应用程序仍然没有收到通知。 我提到了一些问题,但没能解决这个问题。iO

  • 当我的应用程序处于打开状态时,我正在通过onMessageReceived(Remotemessage mesg)获得推送通知。如果我的应用程序处于Kilded状态,我将收到推送通知,但不是从onMessageReceived()获得的。 意思是,在收到推送通知后,根据通知中的数据,我需要重定向页面,当通知点击时。如果应用程序在前景工作良好。当我杀人的时候,我在托盘上收到通知,但当我点击通知时,它

  • 我可能已经阅读了所有其他与此相关的SO线程,但我找不到答案,而且在Android监视器中也没有日志跟踪。有人对此有提示吗?