簡體   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