我正在按照本教程进行操作,并且正在使用FCM发送推送通知,但是由于某些原因,该代码似乎无法正常工作。 在onMessageRecieved()中,im会构建通知,使电话振动并显示通知,并且由于某种原因电话会振动,但未显示通知。 这是代码:
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);
notificationManager.notify(0,notificationBuilder.build());
如果有人可以告诉我怎么了,那太好了..谢谢:)
答案 0 :(得分:0)
根据您的代码以及未显示通知的事实,我假设您正在使用Android 8.0以上版本的设备进行测试。
对于Android 8.0(Oreo)或更高版本,您需要使用通知通道,才能显示通知。有关更多详细信息,请参见this link。
答案 1 :(得分:0)
对于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());