【发布时间】:2019-02-03 03:26:40
【问题描述】:
我向安卓手机发送推送通知。 除了一部手机(Samsung Galaxy Note 5)外,几乎所有手机都能接收并显示通知。
手机显示通知图标并同时响起。 您可以在 url 后面看到这个问题(参考 7sec 和 22sec): 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;
}
}
【问题讨论】:
-
请使用 MyFirebaseMessagingService 服务的代码编辑问题。
-
@Yasiru Nayanajith 我添加了它。
-
AndroidManifest.xml 中不需要“FCMInstanceService”服务
标签: android android-notifications