【问题标题】:Push Notification Service is not running when the app is not in the background当应用程序不在后台时,推送通知服务未运行
【发布时间】:2017-03-13 06:18:20
【问题描述】:

我对在我的应用程序上集成 FCM(Firebase 云消息传递)推送通知有点困惑。

通常,除了 Intent 服务,其他服务不会在中间停止。我通过扩展到 FirebaseMessagingService 创建了我的消息接收服务,如下所示

public class MyFirebaseMessagingService extends FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.e(TAG, "From: " + remoteMessage.getFrom());

    if (remoteMessage == null)
        return;

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.e(TAG, "NotificationBean Body: " + remoteMessage.getNotification().getBody());

        //This method is responsible for handling notification 
        handleNotification(remoteMessage.getNotification().getBody());
     }

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());

        try {
            JSONObject json = new JSONObject(remoteMessage.getData().toString());
            handleDataMessage(json);
        } catch (Exception e) {
            Log.e(TAG, "Exception: " + e.getMessage());
        }
    }
}
}

并在清单上注册服务如下:

<service android:name=".service.MyFirebaseMessagingService">
     <intent-filter>
         <action android:name="com.google.firebase.MESSAGING_EVENT" />   
     </intent-filter>
</service>

当应用程序处于活动状态并且也在后台运行时,此服务会运行。但是当应用程序不在后台时,服务不再运行。

我已经在 Main Activity 上注册了如下服务

@Override
    protected void onResume() {
        super.onResume();

        // register GCM registration complete receiver
        LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
                new IntentFilter(Config.REGISTRATION_COMPLETE));

        // register new push message receiver
        // by doing this, the activity will be notified each time a new message arrives
        LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
                new IntentFilter(Config.PUSH_NOTIFICATION));

        // clear the notification area when the app is opened
        NotificationUtils.clearNotifications(getApplicationContext());
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

让我知道我的代码有什么问题。 (可能不是)。为什么通知不在后台运行。我该如何解决这个问题?

提前致谢。

【问题讨论】:

  • 我已经找到了解决这个问题的方法。该解决方案在华为手机上运行良好。但不适用于Xiomi RedMe note 3。解决方案链接在这里itechify.com/2016/02/01/…
  • 我们不能以编程方式解决这个问题

标签: android push-notification google-cloud-messaging firebase-cloud-messaging


【解决方案1】:

像这样使用你的服务器数据或 api

       {
"to" : "deviceToken",

"notification" : {
  "body" : "Pass body here",
  "title" : "Title n",
  "icon" : " icon ",
  "sound" : "notification sound "
}

}

//例如。

$fields=array('to'=>fdfdfdfdsfdsdfdfdsfdsfdfdsfd" ,'notification'=>array('title'=>'mytitle','body'=>$title,'click_action'=>'abc','icon'=>'ic_stat_final','sound'=>'default','color'=>'#00aff0'),'data'=>array('ghmid'=>$hgmid,'page'=>$page));

【讨论】:

  • 感谢您的回复。我的想法是从客户端询问,而不是在服务器部分询问。即使应用程序在后台或实时运行,客户端也能完美运行。问题是当应用程序未在后台运行时,会跳过推送通知。
  • 如果您的应用在后台 onMessageReceived 将不会调用,因此您无法在客户端处理
  • FirebaseMessagingService 类仅在应用打开且不在后台时调用
  • 但它不是一个意图服务,它是一个服务类。如果它是一个服务类,那么它应该通过服务类在免费进程上为应用程序提供服务。否则,我们将写作作为服务毫无意义。如果我说错了,请纠正我。
  • 是的,你是对的,但我尝试了同样的方法,但我没有发现任何有用的细节,我得到了相同的解决方案,它也可以正常工作
【解决方案2】:

如果您的数据属于通知类型,即“通知”,则不应在应用程序处于后台时调用 onReceived 方法。虽然当应用程序进入前台时可以检索它。如果它是数据类型,那么它将被调用。将您的类型更改为“数据”而不是通知。此外,当您将数据更改为“数据”类型时,您的 getNotification() 方法可能不起作用,并且应用程序将获得空指针异常。服务器数据也可以是同时具有“数据”和“通知”的类型。

【讨论】:

    【解决方案3】:

    将 json 对象键从通知更改为数据

    例如

    {
    “to”: “device_ID/fcmID”,
    “notification”: {
    “body”: “great match!”,
    “title”: “Portugal vs. Denmark”,
    “icon”: “myicon”
    }
    }
    

    改成

    {
    “to”: “device_ID/fcmID”,
    “data”: {
    “Nick”: “abc”,
    “body”: “nfjwhhwruguig”,
    “Room”: “cnrughwriugg”
    },
    }
    

    客户端应用程序在onMessageReceived() 中接收数据消息,无论应用程序是在前台还是后台。

    参考:https://blog.talentica.com/2017/01/20/firebase-cloud-messaging-in-android/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-07
      相关资源
      最近更新 更多