我尝试将云消息连接到我的应用程序,尝试了所有解决方案后,通知仍然没有到达。当我尝试在控制台中发送内容时,只写了这样的内容: 这是manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.myapp">
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:noHistory="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Myapp"
tools:targetApi="31">
<activity
android:name=".SignInActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SignUpActivity"
android:exported="true">
<intent-filter>
<action android:name=".SignUpActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".CreatedWorkoutActivity"
android:exported="false">
<intent-filter>
<action android:name=".CreatedWorkoutActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".WorkoutMakerActivity"
android:exported="true">
<intent-filter>
<action android:name=".WorkoutMakerActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:exported="false">
</activity>
<activity
android:name=".Pop"
android:theme="@style/AppTheme.CustomTheme"
>
</activity>
<service android:name=".FCMNotificationService"
android:permission="com.google.android.c2dm.permission.SEND"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
</intent-filter>
</service>
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id" />
</application>
</manifest>
FCMNotificationService.java:
package com.example.myapp;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class FCMNotificationService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
System.out.println("я тут");
if (remoteMessage.getNotification()!=null){
// Show the notification
String notificationBody = remoteMessage.getNotification().getBody();
String notificationTitle = remoteMessage.getNotification().getTitle();
sendNotification (notificationTitle, notificationBody);
}
}
private void sendNotification(String title, String body) {
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_IMMUTABLE);
String channelId = "fcm_default_channel";
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
我添加的 MainActivity 部分是为了解决此问题“应用程序尚未创建 AndroidManifest.xml 中设置的通知通道。将使用默认值。”:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager = getSystemService(NotificationManager.class);
CharSequence name = getString(R.string.default_notification_channel_name);
String description = getString(R.string.default_notification_channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(getString(R.string.default_notification_channel_id), name, importance);
channel.setDescription(description);
// Optionally set other channel settings such as lights, vibration, etc.
notificationManager.createNotificationChannel(channel);
}
我尝试添加很多东西来体现,但没有任何帮助。
从 API 级别 33 开始,您需要用户的许可才能在 Android 中显示推送通知。
参考:https://developer.android.com/develop/ui/views/notifications/notification-permission
在清单中声明权限,如下所示:
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
来自文档:
如果您的应用面向 Android 13 或更高版本,则您的应用可以完全控制何时显示权限对话框。利用这个机会向用户解释为什么应用程序需要此权限,鼓励他们授予该权限。
如果您的应用程序的目标版本为 12L(API 级别 32)或更低,则当您的应用程序在创建通知通道后首次启动 Activity 时,或者当您的应用程序启动 Activity 然后创建其第一个通知通道时,系统会显示权限对话框。这通常是在应用程序启动时发生。