我正在关注本教程,我正在使用FCM发送推送通知,但由于某些原因,代码似乎不起作用。在onMessageRecieved()我建立通知,制作手机……
根据您的代码以及未显示通知的事实,我假设您正在使用Android版本高于8.0的设备进行测试。
对于Android 8.0(Oreo)及更高版本,您需要使用通知渠道才能显示通知。参考 这个链接 更多细节。
对于Android 8及更高版本,请使用下面的通知渠道
Intent intent=new Intent(this, WelcomeActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent=PendingIntent.getActivity(this ,0 ,intent ,PendingIntent.FLAG_ONE_SHOT); NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this); notificationBuilder.setContentTitle(getString(R.string.app_name)); notificationBuilder.setContentText(message); Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); notificationBuilder.setSound(soundUri); notificationBuilder.setSmallIcon(R.mipmap.ic_launcher); notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(),R.mipmap.ic_launcher)); notificationBuilder.setAutoCancel(true); Vibrator v=(Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE); v.vibrate(1000); notificationBuilder.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, "Membership Alerts", NotificationManager.IMPORTANCE_DEFAULT); notificationManager.createNotificationChannel(channel);*/ CharSequence name = getString(R.string.notification_channel_name); String description = getString(R.string.notification_channel_description); int importance = NotificationManager.IMPORTANCE_DEFAULT; NotificationChannel channel = new NotificationChannel(channelId, name, importance); channel.setDescription(description); notificationManager.createNotificationChannel(channel); } notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());