【发布时间】:2018-12-03 19:18:39
【问题描述】:
我正在为聋人和盲人创建一个应用程序(他们可以在屏幕上看到颜色,但看不到细节)。每当有人按门铃时,我想振动智能手表并显示某种颜色。门铃将通过节点通过Firebase通过节点向用户发送消息,请参见下面的示例:
import admin from 'firebase-admin';
// tslint:disable-next-line:no-var-requires
const serviceAccount = require('../../../firebase.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: 'https://example.firebaseio.com',
});
export function sendMessageToUser(
token: string,
payload: { data: { color: string; vibration: string; text: string } },
priority: string,
) {
const options = {
priority,
timeToLive: 60 * 60 * 24,
};
return new Promise((resolve, reject) => {
admin
.messaging()
.sendToDevice(token, payload, options)
.then(response => {
console.log(response);
resolve(response);
})
.catch(error => {
console.log('error', error);
reject(error);
});
});
}
智能手表通过以下服务接收firebase消息:
public class HapticsFirebaseMessagingService extends FirebaseMessagingService {
private SharedPreferences sharedPreferences;
@Override
public void onCreate() {
super.onCreate();
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
}
@Override
public void onNewToken(String token) {
super.onNewToken(token);
sharedPreferences.edit().putString("fb", token).apply();
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> data = remoteMessage.getData();
String color = data.get("color");
String vibration = data.get("vibration");
String text = data.get("text");
Intent dialogIntent = new Intent(this, AlarmActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bundle bundle = new Bundle();
bundle.putString("color", color);
bundle.putString("vibration", vibration);
bundle.putString("text", text);
dialogIntent.putExtras(bundle);
startActivity(dialogIntent);
}
/**
* Get the token from the shared preferences.
*/
public static String getToken(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context).getString("fb", "empty");
}
}
当智能手表连接到计算机时,这可以正常工作,但是当我将智能手表与计算机断开连接时,它可以工作几分钟。但是几分钟后onMessageReceived 没有被调用,也不会打开活动。为什么 de service 不再接收消息?以及如何修复它,以便服务始终收到消息。消息总是需要尽可能快地传递给用户,因为它被用作聋人和盲人的门铃。
【问题讨论】:
标签: android node.js firebase firebase-cloud-messaging android-service