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

时间:2017-03-13 06:18:20

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

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

通常情况下,中间不再停止其他服务的意图服务。我通过扩展到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();
    }

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

提前致谢。

3 个答案:

答案 0 :(得分:0)

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

       {
"to" : "deviceToken",

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

}

// for 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));

答案 1 :(得分:0)

如果您的数据属于通知类型,即“通知”,那么当app在后台时,不应该调用onReceived方法。虽然它可以在应用程序到达前台时重新启动。如果它是类型数据,那么它将被调用。将您的类型更改为“数据”而不是通知。此外,当您更改数据以键入“数据”时,您的getNotification()方法可能无法正常工作,应用程序将获得空指针异常。服务器数据也可以是既有“数据”又有“通知”的类型。

答案 2 :(得分:0)

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

e.g

{
“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/