简体   繁体   English

无法构建 android 推送通知

[英]Can not build android push notifications

I have an android project,我有一个 android 项目,

this is my FirebaseMessagingService.java这是我的 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());
    }
}

I am able to receive the notification as i can see from the logcat:我可以从 logcat 中看到通知:

2020-06-22 10:54:30.848 17118-18005/com.example.bletamodulet D/Msg: Message received [Android test notification] 
but for some reaseon i cant build it, it doesnt show to the user as a notification, i tonly shows in the logcat

If you use Android 8.0 or above for test that you need create channel:如果您使用 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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM