jaz*_*bpn 20 push-notification android-notifications firebase-cloud-messaging android-studio-3.0 android-8.0-oreo
我们的应用程序现在有 targetSdkVersion 26 ( Android 8 ) 并且应用程序使用 FCM 推送通知。当应用程序处于前台状态时,推送通知运行良好。当应用程序不在前台状态时单击通知时会出现此问题。我刚刚在清单中添加了元数据,但仍然出现相同的错误。
AndroidManifest.xml
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel"
android:value="@string/default_notification_channel_id"/>
Run Code Online (Sandbox Code Playgroud)
MyFirebaseMessagingService.java
/**
* Create and show a simple notification containing the received FCM message.
*/
private void sendNotification(NotificationModel notificationModel)
{
Intent intent;
if (notificationModel.getAppLink() != null)
{
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(notificationModel.getAppLink()));
} else
{
intent = new Intent(this, NoticeActivity.class);
}
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
if (notificationModel.getAppLink() != null)
{
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
} else
{
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
}
notificationBuilder.setContentTitle(notificationModel.getTitle())
.setContentText(notificationModel.getMessage())
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
{
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
assert notificationManager != null;
notificationBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
notificationManager.createNotificationChannel(notificationChannel);
}
assert notificationManager != null;
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
Run Code Online (Sandbox Code Playgroud)
小智 20
我知道这已经晚了,但我希望这对您或至少其他人有所帮助。这里之前已经回答过了。
基本上,您的元数据名称是错误的。
代替
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/notification_channel_id" />
Run Code Online (Sandbox Code Playgroud)
它应该是
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id" />
Run Code Online (Sandbox Code Playgroud)
更新: 添加缺少的报价
较新的答案和提示
元数据标签:
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/notification_channel_id" />Run Code Online (Sandbox Code Playgroud)
正在AndroidManifest.xml 上的应用程序标记内部,例如:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.domain.appname">
<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="@mipmap/ic_launcher" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id"/> Run Code Online (Sandbox Code Playgroud)
还有一个名为“app/src/main/res/values/strings.xml”的文件,您需要在资源标记内添加以下行:
<string name="default_notification_channel_id">com.domain.appname.urgent</string>
Run Code Online (Sandbox Code Playgroud)
值“com.domain.appname.urgent”需要与您默认创建的通道相同。
strings.xml 如下所示:
<string name="default_notification_channel_id">com.domain.appname.urgent</string>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
36973 次 |
| 最近记录: |