【问题标题】:How to edit notification in background came from Firebase?如何在后台编辑来自 Firebase 的通知?
【发布时间】:2016-12-29 23:32:18
【问题描述】:

我有一个应用从 Firebase 接收通知,但它没有声音和应用图标。

在 onMessageReceived() 方法中,我可以编辑通知,但它在后台不起作用。

解决办法是什么?

【问题讨论】:

  • 你会添加你的代码
  • 'code'@Override public void onMessageReceived(RemoteMessage remoteMessage) { Intent msgrcv = new Intent(this,MyServer.class); msgrcv.putExtra("消息",remoteMessage.getNotification().getBody());发送广播(msgrcv); }

标签: android android-studio firebase firebase-cloud-messaging


【解决方案1】:

从您的服务器发送:

  {
    "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "notification" : {
      "body" : "Hello World!",
      "title" : "My app",
      "icon" : "myicon"
      "sound":"mysound"
    }

  }

声音文件必须在/res/raw/,图标在/res/drawable。

从 Firebase 控制台

无法在您的应用被终止时编辑通知,因为 android 系统会处理通知。

【讨论】:

  • 请提供完整示例
  • 您只需要从您的服务器发送带有参数声音和图标的 json,或者从带有此参数的 firebase 发送 json,通知中的系统 android 将播放声音并显示您保存在 /res/raw/ 和/res/drawable。
  • 你看到whatsapp了吗?我想让我的通知变成这样
【解决方案2】:

嘿,检查一下这段代码。

 private void showNotification(NotificationCompat.Builder mBuilder, int icon, String title, String message, String timeStamp, PendingIntent resultPendingIntent, Uri alarmSound)
{
    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
    inboxStyle.addLine(message);
    Notification notification;
    notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
            .setAutoCancel(true)
            .setContentTitle(title)
            .setContentIntent(resultPendingIntent)
            .setSound(alarmSound)
            .setStyle(inboxStyle)
            .setWhen(getTimeMilliSec(timeStamp))
            .setSmallIcon(R.mipmap.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
            .setContentText(message)
            .build();

    NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(Config.NOTIFICATION_ID, notification);
}

【讨论】:

  • 我在哪里可以使用这个
  • 您可以在 onMessageReceived() 方法中使用此代码。
  • 但 onMessageReceived() 方法未在后台调用
  • firebase 向 onMessageReceived() 发送通知,然后我们可以使用给定的代码编辑通知。如果您打算在 Firebase 本身中进行更改,那么根据我的知识,这是不可能的在那里编辑。
  • 您只能在 onMessageReceived() 中编辑通知。
猜你喜欢
  • 2021-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-03
  • 2019-04-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多