Java 无法生成android推送通知
我有一个android项目 这是我的FirebaseMessagingService.javaJava 无法生成android推送通知,java,android,firebase,Java,Android,Firebase,我有一个android项目 这是我的FirebaseMessagingService.java public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService{ @Override public void onMessageReceived(RemoteMessage remoteMessage) { super.onMess
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService{
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Map<String, String> data = remoteMessage.getData();
String myCustomKey = data.get("message");
Log.d("Msg", "Message received ["+myCustomKey+"]");
// Create Notification
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1410, intent,
PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.add)
.setContentTitle("Message")
.setContentText(myCustomKey)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1410, notificationBuilder.build());
}
}
如果您使用Android 8.0或更高版本进行测试,则需要创建频道:
////
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel("channelId", "ChannelName", NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "channelId")
.setSmallIcon(R.drawable.add)
.setContentTitle("Message")
.setContentText(myCustomKey)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
notificationManager.notify(1410, notificationBuilder.build());
////
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel("channelId", "ChannelName", NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "channelId")
.setSmallIcon(R.drawable.add)
.setContentTitle("Message")
.setContentText(myCustomKey)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
notificationManager.notify(1410, notificationBuilder.build());