【发布时间】:2018-07-24 03:29:00
【问题描述】:
我想显示已经从 Firebase 发送的多个通知(我使用的是服务器,php-not 控制台),当应用程序关闭并再次打开时,它可以显示多个通知。但我只收到一条通知,点击时会显示最新消息,我希望通知保留在主要活动中。
主要活动
TextView text = (TextView) findViewById(R.id.data);
if (text != null) {
Bundle extras = getIntent().getExtras();
StringBuilder keys = new StringBuilder();
if (extras != null) {
for (String key : extras.keySet())
keys.append(key + " = " + extras.getString(key) + "\n");
}
text.setText("" + keys.toString());
}
Firebase 消息传递服务
private void sendPushNotification(JSONObject json) {
Log.e(TAG, "Notification JSON " + json.toString());
try {
JSONObject data = json.getJSONObject("data");
String title = data.getString("title");
String message = data.getString("message");
String imageUrl = data.getString("image");
MyNotificationManager mNotificationManager = new MyNotificationManager(getApplicationContext());
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("title",title);
intent.putExtra("message",message);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0/*request code*/,intent,PendingIntent.FLAG_ONE_SHOT);
if(imageUrl.equals("null")){
mNotificationManager.showSmallNotification(title, message, intent);
}
else{
mNotificationManager.showBigNotification(title, message, imageUrl, intent);
}
}
catch (JSONException e) {
Log.e(TAG, "Json Exception: " + e.getMessage());
}
catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
通知管理器
public void showSmallNotification(String title, String message, Intent intent) {
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
mCtx,
ID_SMALL_NOTIFICATION,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx,"channelId");
Notification notification;
notification = mBuilder.setSmallIcon(R.mipmap.ic_launcher).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentIntent(resultPendingIntent)
.setContentTitle(title)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(), R.mipmap.ic_launcher))
.setContentText(message)
.setDefaults(DEFAULT_SOUND | DEFAULT_VIBRATE)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(ID_SMALL_NOTIFICATION, notification);
}
很抱歉,如果之前已经问过这个问题,但我已经用谷歌搜索过,没有找到任何我想要的类似案例,但这可能是由于我的词汇量有限。
【问题讨论】:
标签: android firebase notifications firebase-cloud-messaging