私は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サービスはすべて削除された後、期待通りに動作します。
また、通知とデータのキーをプッシュのペイロードに使用します。
おかげ
スティルは機能しません。私はそのペイロードを試しました。アイコンやサウンドについて何かできますか? –
@MichaelChaves私の編集ansを確認してください。これの結果は何か教えてください。 –
アプリケーションがフォアグラウンドまたはバックグラウンドの場合、正常に動作します。私がアプリを殺すと何も起こらない –