-4
我會嘗試發送數據消息到我的應用程序,如這種格式。如何顯示安卓設備屏幕關閉時彈出通知
{
"to": "token",
"data": {
"title":"Title...",
"body":"Body...",
"text":"text"
}
}
然後在這個JSON之後,我正在用我的設備在下面的方法。
@Override
public void onMessageReceived(RemoteMessage message) {
Map<String, String> map = message.getData();
JSONObject obj = new JSONObject();
for (Object o : map.keySet()) {
String key = o.toString();
String value = map.get(key);
obj.put(key, value);
}
Intent resultIntent = new Intent();
showNotificationMessage(getApplicationContext(), "Test ...", obj.toString(), resultIntent);
}
public void showNotificationMessage(String title,
String message,
Intent intent) {
// Check for empty push message
if (TextUtils.isEmpty(message) && TextUtils.isEmpty(title)) {
return;
}
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(mContext, 0, intent, 0);
notificationSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
showSmallNotification(title, message, resultPendingIntent, notificationSoundUri);
playNotificationSound();
}
private void showSmallNotification(String title,
String message,
PendingIntent resultPendingIntent,
Uri alarmSound) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.addLine(message);
Notification notification;
notification = mBuilder.setSmallIcon(R.mipmap.ic_launcher)
.setTicker(title)
.setShowWhen(true)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setSound(alarmSound)
.setStyle(inboxStyle)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher))
.setContentText(message)
.build();
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(new Random().nextInt(10000), notification);
}
使用這個標題和正文我想顯示彈出式通知我形成設備與設備屏幕關閉我怎麼能在我的設備彈出通知的時間。
這是有效的回答,但在棉花糖下工作,你有任何建議以上棉花糖 –