Android推送通知显示啥
Posted
技术标签:
【中文标题】Android推送通知显示啥【英文标题】:Android Push notification Shows NothingAndroid推送通知显示什么 【发布时间】:2021-12-01 18:39:33 【问题描述】:我正在使用 FCM,一切正常,通知是使用“数据”发送的,但是在显示时会发生这种情况。我真的不知道该怎么办了。
public class CloudMessaging extends FirebaseMessagingService
String NOTIFICATION_CHANNEL_ID = "Messages_Channel_ID";
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage)
super.onMessageReceived(remoteMessage);
createNotificationChannel();
Map<String, String> data = remoteMessage.getData();
final String title = data.get("title");
final String body = data.get("body");
final String conversation = data.get("conversation");
if (conversation == null) return;
NotificationCompat.Builder builder = new
NotificationCompat.Builder(getApplicationContext(), NOTIFICATION_CHANNEL_ID);
builder.setColor(Color.CYAN)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
private void createNotificationChannel()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
CharSequence name = getString(R.string.message_channel_name);
String description = getString(R.string.message_channel_description);
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel messagesChannel = new
NotificationChannel(getString(R.string.Message_Notification_ID), name, importance);
messagesChannel.setDescription(description);
messagesChannel.enableVibration(true);
messagesChannel.setVibrationPattern(new long[]1000, 200, 500);
messagesChannel.enableLights(true);
messagesChannel.setLightColor(Color.CYAN);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(messagesChannel);
后端服务器,在 Firebase 云消息传递中:
const data =
token: context.params.token,
data:
title: document.username,
body: document.message,
conversation: document.conversationid,
,
android:
notification:
channel_id: "Messages_Channel_ID",
,
,
;
All the notifications in all sdk's return like this
编辑 1:
做了一些测试,我意识到,onreceived 方法永远不会被调用,即使是在后台或前台!即使在清单中声明了类!但是只显示空白通知,因为我在清单中也声明了颜色和图标默认值。如果它没有声明,可能什么都不显示。
【问题讨论】:
【参考方案1】:很奇怪,onMessageReceived方法至少应该在前台调用。请检查通知的样式并尝试我在此repository 中的示例代码。
public class PushNotificationHandbookService extends FirebaseMessagingService
public static final String TAG = "PushHandbookService";
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage)
super.onMessageReceived(remoteMessage);
// You will receive the push notifications here!
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null)
Log.d(TAG, "Title: " + remoteMessage.getNotification().getTitle() +
"Body: " + remoteMessage.getNotification().getBody());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0)
Log.d(TAG, "Title: " + remoteMessage.getData().get("title") +
"Body: " + remoteMessage.getData().get("body"));
sendNotification(remoteMessage);
...
private void sendNotification(RemoteMessage remoteMessage)
String title = remoteMessage.getData().get("title");
String messageBody = remoteMessage.getData().get("body");
String score = remoteMessage.getData().get("score");
String country = remoteMessage.getData().get("country");
Intent intent = new Intent(this, PushReceiverActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("score", score);
intent.putExtra("country", country);
@SuppressLint("UnspecifiedImmutableFlag")
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.default_notification_channel_id);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_stat_name)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.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,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
notificationManager.notify(0, notificationBuilder.build());
...
使用这样的消息:
var message =
notification:
title: title,
body: text
,
data:
title: title,
body: text,
score: '4.5',
country: 'Canada'
,
android:
notification:
priority: 'high',
sound: 'default',
clickAction: '.PushReceiverActivity'
,
,
tokens: registrationTokens
;
【讨论】:
以上是关于Android推送通知显示啥的主要内容,如果未能解决你的问题,请参考以下文章