Firebase推送通知在后台震动 [英] Firebase push notifications vibrate in background

查看:1934
本文介绍了Firebase推送通知在后台震动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为Android安装了 com.google.firebase:firebase-messaging:10.0.1 ,试图在应用程序处于后台时使设备振动并收到一个推送通知。
我将下面的json发送给FCM服务器:

$ $ p $ to:'主题/ ...',
通知:{
title:'New Message',
body:'top security body',
sound:'mysound',
action_click:'top_security'
}

当应用处于前台时, onMessageReceived 方法 FirebaseMessagingService 被触发,我可以添加振动。但是,当应用程序在后台, onMessageReceived 不会被调用,我无法控制振动。我的第一个想法是使用数据块代替背景和前景,但是如果没有通知 block。



那么如何在应用程序处于后台时添加振动来推送通知?
P.S.当我在设备上打开振动模式时,推送通知会导致震动,而不是播放声音。 使用 data 有效载荷,而不是通知有效载荷,因为数据有效载荷将触发 onMessageReceived 该应用程序是背景或前景。您的请求将如下所示:

  data:{
title:'New Message',
body: '最高安全机构',
声音:'mysound',
action_click:'top_security'
}

要从 data 内的 onMessageReceived 内部检索数据,请调用 remoteMessage.getData(); 这将返回结果在 Map< String,String>



例如:

  Map< String,String> data = remoteMessage.getData(); 
String title = data.get(title);
String body = data.get(body);

然后根据需要自定义通知。



希望这有助于:)


I'm using com.google.firebase:firebase-messaging:10.0.1 for Android and trying to make the device vibrate while application is in background and a push notification is received. I send following json to FCM server:

to: 'topics/...',
notification: {
    title: 'New Message',
    body: 'top security body',
    sound: 'mysound',
    action_click: 'top_security'
}

When the app is in foreground, the onMessageReceived method of FirebaseMessagingService is triggered and I can add vibration. But, when the app is in background, onMessageReceived is not called and I have no control on the vibration. My first thought was to use data block instead both for background and foreground, but iOS client does not receive pushes in background if there is no notification block.

So how can I add vibration to push notification when the app is in background? P.S. When I turn on vibrate mode on the device, the push notification cause vibrating instead of playing the sound.

解决方案

Use data payload instead of notification payload because data payload will trigger onMessageReceived even though the app is background or foreground. Your request will look like this

data: {
    title: 'New Message',
    body: 'top security body',
    sound: 'mysound',
    action_click: 'top_security'
}

To retrieve the data from data payload inside onMessageReceived, call remoteMessage.getData(); which will return the result in a Map<String,String>

For example:

Map<String, String> data = remoteMessage.getData();
String title = data.get("title");
String body = data.get("body");

Then customize the notification as you want with that data.

Hope this helps :)

这篇关于Firebase推送通知在后台震动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆