Firebase具有默认的简单通知布局,android默认为一次。如何更改为自定义布局,并在生成时显示通知。如何使用自定义布局显示Firebase通知?
回答
在FirebaseMessaging服务编写以下
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().size() > 0) {
try {
JSONObject jsonObject = new JSONObject(remoteMessage.getData());
Log.e("Tag",remoteMessage.getData().toString());
sendNotification(remoteMessage.getData().toString());
} catch (Exception e) {
}
}
private void sendNotification(String msg) {
Intent intent = new Intent(this, NewTransactionsHistActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, new Random().nextInt(100) , intent,
PendingIntent.FLAG_ONE_SHOT);
long when = System.currentTimeMillis();
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(this);
mNotifyBuilder.setVibrate(new long[] { 1000, 1000,1000,1000,1000,1000});
boolean lollipop = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP);
if (lollipop) {
mNotifyBuilder = new NotificationCompat.Builder(this)
.setContentTitle(getString(R.string.app_name))
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg)
.setColor(Color.TRANSPARENT)
.setLargeIcon(
BitmapFactory.decodeResource(
getResources(),
R.drawable.rlogo))
.setSmallIcon(R.drawable.ic_icon_lollipop)
.setWhen(when).setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
} else {
mNotifyBuilder = new NotificationCompat.Builder(this)
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentTitle(getString(R.string.app_name)).setContentText(msg)
.setSmallIcon(R.drawable.rlogo)
.setWhen(when).setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
}
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(new Random().nextInt(100) /* ID of notification */, mNotifyBuilder.build());
}
** onMessageReceived **方法触发器当应用程序在前台。 如果应用程序在后台运行,它将不会触发。 检查[本答](https://stackoverflow.com/questions/37358462/firebase-onmessagereceived-not-called-when-app-in-background)。 –
在你的服务器端,删除notification
attirbute。
当您发送没有notification
属性的通知时,Firebase将不处理通知。 然后,您可以扩展FirebaseMessagingService
来处理通知。 不要忘记在清单中注册服务。
我收到通知但我想更改它与自定义布局如何处理它 –
检查此https://www.laurivan.com/android-notifications-with-custom-layout/ –
- 1. 自定义通知布局
- 2. 未使用自定义服务器显示Android Firebase通知
- 3. 自定义firebase通知
- 4. X10中的自定义通知布局
- 5. 如何使用Malcom Android SDK显示自定义通知消息?
- 6. 自定义线性布局不显示
- 7. 不显示自定义布局
- 8. 如何在android中创建自定义通知布局?
- 9. Firebase通知不会显示使用PHP
- 10. Android自定义通知不显示
- 11. 自定义推送通知不显示
- 12. 如何通过JIDE仪表板使用自定义布局
- 13. 通知未显示完整布局
- 14. 如何在MultiSelectListPreference顶部显示自定义布局
- 15. ANDROID - 如何在标记下方显示自定义布局?
- 16. Android通知:如何使用自定义位图发布setSmallIcon
- 17. 如何从另一个自定义XML布局引用自定义XML布局?
- 18. 自定义布局感知控件
- 19. 如何动态显示自定义标题时使用布局和部分
- 20. 推送通知不显示自定义通知
- 21. 单击Firebase通知负载通知时打开自定义Intent
- 22. 自定义布局
- 23. 如何从android firebase通知中获取自定义数据?
- 24. 自定义布局的微调不显示任何Android
- 25. 在布局上显示自定义烤面包,例如AppMsg
- 26. 只更新自定义布局的Android通知的一部分
- 27. 远程视图中动态布局的自定义通知
- 28. 果冻豆的自定义通知布局android
- 29. 自定义通知布局不覆盖整个宽度
- 30. 修改标签后面的Android自定义通知布局
U表示添加图片或图标,您的通知 –
NOP兄弟创建定制商品通知或者可以说,倒塌的通知。 –