Android推送通知日志已接收但未在设备上显示
我正在从AWS向android发送一些推送通知 通知过程正在注册设备,我确实收到了测试通知,但只显示在日志上,而不是像任何其他通知一样显示在屏幕顶部的通知栏上Android推送通知日志已接收但未在设备上显示,android,amazon-web-services,amazon-sns,Android,Amazon Web Services,Amazon Sns,我正在从AWS向android发送一些推送通知 通知过程正在注册设备,我确实收到了测试通知,但只显示在日志上,而不是像任何其他通知一样显示在屏幕顶部的通知栏上 public void onMessageReceived(RemoteMessage remoteMessage) { // ... // TODO(developer): Handle FCM messages here. Log.d("mako", "A From: " + remoteMessage.
public void onMessageReceived(RemoteMessage remoteMessage) {
// ...
// TODO(developer): Handle FCM messages here.
Log.d("mako", "A From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d("mako", "B Message data payload: " + remoteMessage.getData());
if (/* Check if data needs to be processed by long-running job */ true) {
// For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
// scheduleJob();
} else {
// Handle message within 10 seconds
// handleNow();
}
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d("mako", "C Message Notification Body: " + remoteMessage.getNotification().getBody());
}
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
}
因此,我发送了一个测试通知:
{
"GCM": "{ \"data\": { \"message\": \"test message\" } }"
}
我可以在控制台日志中看到消息test:
10-05 14:57:08.827 23062-23296/com.sb.comm D/mako:B消息数据有效负载:{Message=test Message}
但是在小弹出窗口上没有显示,在屏幕上显示实际的推送通知缺少什么
干杯请添加这些代码行以在通知栏上显示通知
Intent intent = new Intent(this, YourActivity.class);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new
NotificationCompat.Builder(this, "my_chanel_id");
notificationBuilder.setSmallIcon(R.drawable.ic_launcher_app);
notificationBuilder.setContentTitle("Titlee");
notificationBuilder.setContentText("anyText");
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSound(uri);
notificationBuilder.setContentIntent(resultPendingIntent);
NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String CHANNEL_ID = "my_channel_01";// The id of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel("mychanelId",
"hyperlocal", importance);
notificationManager.createNotificationChannel(mChannel);
}
notificationManager.notify(message_id, notificationBuilder.build());
您必须像这样在设备上生成通知
public void onMessageReceived(RemoteMessage remoteMessage) {
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("App Name")
.setBadgeIconType(R.drawable.ic_notification)
.setLargeIcon(BitmapFactory.decodeResource(
getResources(),R.drawable.ic_notification))
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentText(remoteMessage.getData().get("body"));
NotificationManager notificationManager =
(NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(141, notificationBuilder.build());
}
在这里,Notification Builder将从您的服务器通知负载中获取数据,并在设备上生成通知。结果表明,这与正文的格式有关,而不是像SNS“JSON消息生成器”所建议的那样 正文应采用以下格式:
{
"GCM": "{ \"notification\": { \"text\": \"test message\" } }"
}
通过通知通道生成通知的代码丢失。@Sahil谢谢,请问这是什么意思?你能在回答中包括吗?thanks@MaKo您需要从代码中创建通知检查她,例如在oreo@MaKo中工作的代码。请参考您从oreo引入的通知通道,因此请确保您注意先前的API版本(也在帖子中解释):