由于Android 12和13的安全策略更加严格,需要进行一些额外的设置才能启用推送通知。以下是一些可能有用的
import com.google.firebase.FirebaseApp;
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
FirebaseApp.initializeApp(this);
}
}
确保在应用程序中包含了Firebase Messaging服务。
添加以下代码到AndroidManifest.xml文件中,以启用自定义通知:
要使用自定义通知样式,请创建一个扩展FirebaseMessagingService的类并重写onMessageReceived()方法。
import androidx.core.app.NotificationCompat;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// 从远程消息中提取通知标题和内容。
String notificationTitle = remoteMessage.getNotification().getTitle();
String notificationBody = remoteMessage.getNotification().getBody();
// 创建自定义通知。
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(notificationTitle)
.setContentText(notificationBody)
.setPriority(NotificationCompat.PRIORITY_HIGH);
// 显示通知。
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
}
}
请注意,这只是解决推送通知问题的一些可能解决方案,具体的解决方案可能因应用程序的不同而有所不同。