我将推送通知发送到android手机。 除了一部手机(三星Galaxy Note 5)以外,几乎所有手机都会收到并显示通知。
电话会显示通知图标并同时打开。 您可以在网址后面看到此问题(请参阅7秒和22秒): https://youtu.be/J6UZm_6mqP0
这是我的应用程序的AndroidMenifest.xml
<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">
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_stat_notification" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id" />
<activity
android:name=".MainActivity"
android:configChanges="keyboard|keyboardHidden|orientation|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".FCMInstanceService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service
android:name=".MyFirebaseMessagingService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
我从哪里开始解决这个问题?
-更多-
MyFirebaseMessagingService.java
package com.smartivt.smartivtmessenger;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.Log;
import com.google.firebase.messaging.RemoteMessage;
import java.util.List;
public class MyFirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
private static final String TAG = "FirebaseMessaging";
private final String CHANNEL_ID = "DEFAULT_CHANNEL";
private final String CHANNEL_NAME = "Default Channel";
private final String GROUP_NAME = "MESSAGE_GROUP";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
NotificationManager notiMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if ( remoteMessage.getData().get("title") != null ) {
Log.d(TAG, "onMessageReceived: " + remoteMessage.getData().get("title"));
Log.d(TAG, "onMessageReceived: " + remoteMessage.getData().get("body"));
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ) {
Notification.Builder notify = new Notification.Builder(getApplicationContext(), CHANNEL_ID);
notify.setSmallIcon(R.drawable.ic_stat_notification);
notify.setContentTitle(remoteMessage.getData().get("title"));
notify.setContentText(remoteMessage.getData().get("body"));
notify.setAutoCancel(true);
notiMgr.notify(NotificationID.getID(), notify.build());
/*
Intent badgeIntent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
badgeIntent.putExtra("badge_count", 5);
badgeIntent.putExtra("badge_count_package_name", getPackageName());
badgeIntent.putExtra("badge_count_class_name", getLauncherClassName());
sendBroadcast(badgeIntent);
*/
Log.d(TAG, "update badge: " + getPackageName() + ", " + getLauncherClassName());
}
else {
Notification.Builder notify = new Notification.Builder(getApplicationContext());
notify.setSmallIcon(R.drawable.ic_stat_notification);
notify.setContentTitle(remoteMessage.getData().get("title"));
notify.setContentText(remoteMessage.getData().get("body"));
notify.setAutoCancel(true);
int id = NotificationID.getID();
Log.d(TAG, "id: " + id);
notiMgr.notify(id, notify.build());
}
}
else if ( remoteMessage.getNotification() != null ) {
Log.d(TAG, "onMessageReceived2: " + remoteMessage.getNotification().getTitle());
Log.d(TAG, "onMessageReceived2: " + remoteMessage.getNotification().getBody());
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ) {
Notification.Builder notify = new Notification.Builder(getApplicationContext(), CHANNEL_ID);
notify.setSmallIcon(R.drawable.ic_stat_notification);
notify.setContentTitle(remoteMessage.getNotification().getTitle());
notify.setContentText(remoteMessage.getNotification().getBody());
notify.setAutoCancel(true);
notiMgr.notify(NotificationID.getID(), notify.build());
}
else {
Notification.Builder notify = new Notification.Builder(getApplicationContext());
notify.setSmallIcon(R.drawable.ic_stat_notification);
notify.setContentTitle(remoteMessage.getNotification().getTitle());
notify.setContentText(remoteMessage.getNotification().getBody());
notify.setAutoCancel(true);
notiMgr.notify(NotificationID.getID(), notify.build());
}
}
}
@Nullable
private String getLauncherClassName () {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
PackageManager pm = getApplicationContext().getPackageManager();
List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
for (ResolveInfo resolveInfo : resolveInfos) {
String pkgName = resolveInfo.activityInfo.applicationInfo.packageName;
if ( pkgName.equalsIgnoreCase(getPackageName())) {
return resolveInfo.activityInfo.name;
}
}
return null;
}
}
答案 0 :(得分:1)
您现在不需要两项服务..... 检查此主题:
FirebaseInstanceIdService is deprecated now.
您不需要这个。
<service
android:name=".MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
像这样使用它。
AndroidManifest.xml
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public static int NOTIFICATION_ID = 1;
@Override
public void onNewToken(String s) {
super.onNewToken(s);
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//Code to create the Notification
}
}
MyFirebaseMessagingService.java
implementation 'com.google.firebase:firebase-core:16.0.3'
implementation 'com.google.firebase:firebase-messaging:17.3.0'
逐步构建
{{1}}