2017-12-27 24 views
2

私はFirebaseからプッシュ通知を受け取るAndroid Appを開発しています。AndroidのFirebaseの背景通知が起動しない

私はトークンを取得し、問題なくPostmanからプッシュ通知を送信できます。

アプリケーションがフォアグラウンドであれば、すべて期待どおりに動作し、私はonMessageReceived(さまざまなペイロードでテスト済み)にペイロードを受け取ります。

しかし、私がアプリを閉じると何も受け取りません。私は多くのペイロードを試しました。そして、私はすべてのドキュメンテーションを読みました(ペイロードのデータと通知の間に違いがあります)。

は、ここに私のプロジェクトで使用する私のクラスです:

1 -

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService { 

private static String TAG = "Android Push App"; 

@Override 
public void onTokenRefresh() { 
    String refreshedToken = FirebaseInstanceId.getInstance().getToken(); 
    sendRegistrationToServer(refreshedToken); 
} 

private void sendRegistrationToServer(String token) { 
    Log.d(TAG, "Did obtained token"); 
    Log.d(TAG, token); 
} 

トークン

3を得るための責任があるクラス - FirebaseMessagingService

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

private static final String TAG = "Android Push App"; 

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    sendNotification("Received notification"); 
} 

private void sendNotification(String messageBody) { 
    Intent intent = new Intent(this, MainActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
      PendingIntent.FLAG_ONE_SHOT); 

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.Builder notificationBuilder = 
      new NotificationCompat.Builder(this) 
        .setSmallIcon(R.drawable.push_icon) 
        .setContentTitle("FCM Message") 
        .setContentText(messageBody) 
        .setAutoCancel(true) 
        .setSound(defaultSoundUri) 
        .setContentIntent(pendingIntent); 

    NotificationManager notificationManager = 
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
} 

2拡張したクラス - 私のマニフェスト:

<uses-permission android:name="android.permission.INTERNET" /> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:roundIcon="@mipmap/ic_launcher_round" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <action android:name="FIREBASE_ACTIVITY" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 

    <service android:name=".push.core.MyFirebaseInstanceIDService"> 
     <intent-filter> 
      <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> 
     </intent-filter> 
    </service> 

    <service 
     android:name=".push.core.MyFirebaseMessagingService" 
     android:enabled="true" 
     android:exported="true"> 
     <intent-filter> 
      <action android:name="com.google.firebase.MESSAGING_EVENT" /> 
     </intent-filter> 
    </service> 

    <meta-data 
     android:name="com.google.firebase.messaging.default_notification_icon" 
     android:resource="@drawable/push_icon" /> 

    <meta-data android:name="com.google.firebase.messaging.default_notification_color" 
     android:resource="@color/colorAccent" /> 

</application> 

4 - MainActivity

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    if (getIntent().getExtras() != null) { 
     for (String key : getIntent().getExtras().keySet()) { 
      Object value = getIntent().getExtras().get(key); 
      Log.d("MainActivity", "Key: " + key + " Value: " + value); 
     } 
    } 
} 

5 - 最後に、私は、ポストマン

POST https://fcm.googleapis.com/fcm/send コンテンツタイプアプリケーション/ JSON 認証キー= AIzaSy(...)KY

このペイロードを試み

JSON Body(試した例):

{ 
"to": "dxe0RDKbP...m9Uc","notification" : { 
    "body" : "great match!", 
    "title" : "Portugal vs. Denmark", 
    "icon" : "push_icon", 
    "sound" : "default" 
}} 

{ 
"to": "dxe0RDKbP...m9Uc","notification" : { 
    "body" : "great match!", 
    "title" : "Portugal vs. Denmark", 
    "icon" : "push_icon", 
    "sound" : "default" 
}} 

そして:

{ 
"to": "d3j-9OJ6R...C6w", 
"notification" : { 
    "title": "title", 
    "body": "body" 
}, 
"data": { 
    "tipo": "normal" 
}} 

はまた、 "優先順位" キーを追加し、それが動作しません。

私は間違っていますか?あなたが私に与えたことができるすべての助けを

感謝:)

UPDATE

は、今では働いています。

FireBaseMessagingServiceと実行中のジオフェンスプッシュ(アプリケーションによって起動される)との間に競合がありました。

このGeofenceサービスはすべて削除された後、期待通りに動作します。

また、通知データのキーをプッシュのペイロードに使用します。

おかげ

答えて

1

試してみてください。この

あなたがAPIをfirebase代わり'data'を使用するように要求してキーJSON 'notification'を入れてはいけません。

使用この

{ 
    "to": "dxe0RDKbP...m9Uc", 
    "data": { 
    "body" : "great match!", 
    "title" : "Portugal vs. Denmark", 
    "icon" : "push_icon", 
    "sound" : "default" 
    } 
} 

あなたが唯一の本文とタイトルを試すことができます EDIT

{ 
    "to": "dxe0RDKbP...m9Uc", 
    "data": { 
     "body" : "great match!", 
     "title" : "Portugal vs. Denmark" 
    } 
} 

編集新しい

マニフェストファイルandroid:stopWithTask="false"サービスプロパティにこれを追加します。

<service 
     android:name="com.yourapp.YourPushService" 
     android:stopWithTask="false" 
     android:enabled="true" 
     android:exported="true"> 
     <intent-filter> 
      <action android:name="com.google.firebase.MESSAGING_EVENT" /> 
     </intent-filter> 
</service> 
+0

スティルは機能しません。私はそのペイロードを試しました。アイコンやサウンドについて何かできますか? –

+0

@MichaelChaves私の編集ansを確認してください。これの結果は何か教えてください。 –

+0

アプリケーションがフォアグラウンドまたはバックグラウンドの場合、正常に動作します。私がアプリを殺すと何も起こらない –

0

jsonから通知ペイロードを削除し、データペイロードを追加する必要があります。これは、Androidには、通知ペイロードが表示されたときに通知を処理する機能が組み込まれているためです。つまり、通知ペイロードが送信され、AndroidがシステムdrayおよびonMessageReceived関数に直接送信するときに呼び出されません。

0

Firebaseの通知に従って、Firebase通知は、受信側アプリケーションのフォアグラウンド/バックグラウンド状態によって異なる動作をします。

onMessageReceivedは、次の例外を除いて、ほとんどのメッセージタイプのために提供されています。あなたのアプリがバックグラウンドで動作しているとき

通知メッセージが配信します。この場合、通知はデバイスのシステムトレイに配信されます。ユーザーが通知をタップすると、デフォルトでアプリランチャーが開きます。

通知とデータペイロードの両方を持つメッセージ(バックグラウンドとフォアグラウンドの両方)。この場合、通知はデバイスのシステムトレイに配信され、データペイロードはランチャーアクティビティの意図の余分に配信されます。

jsonから通知ペイロードを削除する必要があり、アプリがバックグラウンドのときにonMessageReceivedを起動するためにデータペイロードを保持する必要があります。

リンク:https://firebase.google.com/docs/cloud-messaging/android/receive

関連する問題