繁体   English   中英

当应用程序不在后台时,推送通知服务未运行

[英]Push Notification Service is not running when the app is not in the background

我在将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>

当该应用程序在后台运行时,该服务也会运行。 但是,当应用程序不在后台运行时,该服务将不再运行。

我已经在“主要活动”上注册了以下服务

@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();
    }

让我知道代码中有什么错误。 (可能不是)。为什么通知不在后台运行。 我该如何克服这个问题?

提前致谢。

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

       {
"to" : "deviceToken",

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

}

//对于exa。

$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));

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

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

暂无
暂无

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

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