【问题标题】:No notification sound and vibrate and lights when sending notification from firebase in android background在android后台从firebase发送通知时没有通知声音和振动和灯光
【发布时间】:2017-01-14 01:29:24
【问题描述】:

我正在从 firebase 向我的 Android 应用程序发送推送通知。但是当我的应用程序在后台时,firebase onMessageReceived 方法不会被调用,而是 firebase 向系统发送通知以在系统托盘中显示通知。通知出现在系统托盘中,但没有通知声音,即使我在系统设置中允许我的应用程序发出通知声音。 这是我的通知代码 谢谢你

    private void sendNotification(String msg, String title) {
    NotificationManager mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, MainActivity.class), 0);
    Intent i = new Intent(this,MainActivity.class);
    i.putExtra("","");

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(title)
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                    .setContentText(msg);
    //Vibration
    mBuilder.setVibrate(new long[] { 200, 400, 600, 800, 1000 });

    //LED
    mBuilder.setLights(Color.MAGENTA, 1000, 1000);

    //Ton
    mBuilder.setSound(Uri.parse("android.resource://"
            + getApplicationContext().getPackageName() + "/" + R.raw.circles_notif));
    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(0, mBuilder.build());
}

【问题讨论】:

  • 你在AndroidManifest中定义了使用振动的android权限吗?:<uses-permission android:name="android.permission.VIBRATE" />
  • 是的,这不是我的问题,当我单击按钮以测试其工作但在后台不工作时
  • 您是否尝试过使用 NotificationCompat 中的 setDefaults(DEFAULT_ALL) 来检查它是否适用于用户的默认首选项?并移除您为测试而定义的振动、灯光和声音。希望这会有所帮助
  • 是的,我试过了,但同样的问题
  • 我有一个带有振动通知的应用程序。与您的代码的唯一区别是我有这样的代码:new NotificationCompat.Builder(this).setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)).setVibrate(new long[]{400,500,300}); not defined from the mBuilder.

标签: android firebase firebase-cloud-messaging


【解决方案1】:

从您的 Firebase 控制台转到通知> 新消息> 高级选项,并将通知作为自定义数据发送。使用 'title' 和 'body' 作为 Key 并将它们的值设置为您希望在通知中显示的值。当您的应用处于后台或被终止时,这应该调用 onMessageReceived()。

如果您从自己的服务器发送通知,这里是一个示例 json 部分:

{   
    "data": {
      "title": "notification_title",
      "body": "notification_body"
    },
    "to" : "jh578_gsh....jhHGFJ76FH"
}

【讨论】:

  • 你好@sudip Podder,当我想要的前台应用程序没有收到通知时
  • 我没有太多的实践经验,但你可以点击这个链接:stackoverflow.com/questions/5504632/…。看起来它可能会提供您正在寻找的解决方案。
【解决方案2】:

写在override sample of onMessageReceived(),第二行评论说:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    ...
    Not getting messages here? See why this may be: ...goo.gl/39bRNJ
    ...
}

comment-link

解决方案,如相关问题的答案:How to handle notification when app in background in Firebase,可以在Messages with both notification and data payloads 部分的文档中找到:

在接收包含通知和数据负载的消息时,应用的行为取决于应用是在后台还是前台——本质上,它在接收时是否处于活动状态。

  • 在后台时,应用在通知托盘中接收通知负载,并且仅在用户点击通知时处理数据负载。
  • 在前台时,您的应用会收到一个消息对象,其中包含两个可用的有效负载。

【讨论】:

    【解决方案3】:

    这似乎对我有用 YMMV

    {
        "notification": {
            "title": "notification_title",
            "body": "notification_body",
            "sound": "default"
        },
        "to" : "device....id"
    }
    

    虽然这个不会在后台唤醒您的应用,而接受的答案会。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 2019-03-20
      • 1970-01-01
      相关资源
      最近更新 更多