Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法生成android推送通知_Java_Android_Firebase - Fatal编程技术网

Java 无法生成android推送通知

Java 无法生成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

我有一个android项目

这是我的FirebaseMessagingService.java

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());