0

FCM(Firebase cloud messaging)プッシュ通知を自分のアプリケーションに組み込む際にちょっと混乱します。アプリがバックグラウンドにないときにプッシュ通知サービスが実行されていません

通常、インテントサービスではなく、他のサービスがもう途中で停止しません。私は

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

に従うようFirebaseMessagingServiceに拡張することで、サービスの提供を受ける私のメッセージを作成し、次のようにマニフェストにサービスを登録している:このサービスは、アプリは、ライブとで実行されているときに実行される

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

は私が私のコードでは何も悪いことを知ってみましょう従うよう

は私が主な活動でサービスを登録しています。 (おそらくそうではありません)。理由は通知がバックグラウンドで実行されていません。どうすればこの問題を克服できますか?

ありがとうございます。

+0

この問題の解決策を見つけました。このソリューションは、Huaweiの携帯電話でうまく機能しています。しかし、Xiomi RedMeのノートには取り組んでいません。3.ソリューションのリンクはhttp://itechify.com/2016/02/01/how-to-fix-missing-push-notifications-on-huawei-smartphones/ – nifCody

+0

できません。プログラムでこの問題を修正しました – nifCody

答えて

0

はEXAのために、この

 { 
"to" : "deviceToken", 

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

}

//のように、サーバーのデータやAPIを使用しています。

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

返信いただきありがとうございます。私はクライアント側から求めていますが、サーバー側ではありません。クライアントサイドでも、アプリがバックグラウンドでもライブであっても、完璧に動作します。問題は、プッシュ通知がスキップされた背景でアプリケーションが実行されていない場合です。 – nifCody

+0

アプリがバックグラウンドの場合はonMessageReceivedはコールしないので、クライアント側では処理できません –

+0

アプリが開いていて、バックグラウンドではない場合のみFirebaseMessagingServiceクラスが呼び出されます –

0

あなたのデータはタイプ通知すなわち「通知」である場合は、その後onReceived方法は、アプリがバックグラウンドで動作しているときに呼び出されることになっていません。アプリがフォアグラウンドになると、それを取得することができます。それがタイプのデータの場合は、通知の代わりにあなたのタイプを "データ"に変更します。また、データを "データ"と入力するように変更すると、getNotification()メソッドが機能しない可能性があり、アプリケーションはNULLポインタ例外を受け取ります。サーバーデータは、「データ」と「通知」の両方を持つタイプでもかまいません。データへの通知から

0

変化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()にデータメッセージを受信しますまたは背景。

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

関連する問題