因此,我正在尝试处理设备之间的通知,并且此错误使我发疯,我不知道我在哪里做错了。我已经尝试了一切,并竭尽全力解决此问题,我们将不胜感激。谢谢! [编辑:我在这里发布了另一个错误的问题,此问题已解决,但导致了这个新错误]
这是我的 FirebaseMessagingClass
package com.pappu5.navigation;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import com.google.firebase.messaging.RemoteMessage;
public class FirebaseMessaging extends
com.google.firebase.messaging.FirebaseMessagingService {
private String channelId = "com.pappu5.navigation";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String notificationTtile = remoteMessage.getNotification().getTitle();
String notificationBody = remoteMessage.getNotification().getBody();
String clickAction = remoteMessage.getNotification().getClickAction();
String from_user_id = remoteMessage.getData().get("from_user_id");
NotificationCompat.Builder mBuilder = new
NotificationCompat.Builder(this,channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(notificationTtile)
.setContentText(notificationBody)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
int notificationId = (int) System.currentTimeMillis();
Intent intent = new Intent(clickAction);
intent.putExtra("user_id",from_user_id);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
// notificationId is a unique int for each notification that you must define
notificationManager.notify(notificationId, mBuilder.build());
}
这是Firebase index.js 文件
'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification =
functions.database.ref('/Notifications/{user_id}/{notification_id}').
onWrite((change, context) => {
const user_id = context.params.user_id;
const notification_id = context.params.notification_id;
console.log('We have a notification to send to : ',
context.params.user_id);
const fromUser =
admin.database().ref('/Notifications/'+user_id+'/'+notification_id +
'/From').once('value');
return fromUser.then(fromUserResult => {
const from_user = fromUserResult.val();
console.log('You have new notification from : ',from_user);
const use_Query =
admin.database().ref(`/Chat_Profiles/{from_user}/name`).once('value');
const deviceToken =
admin.database().ref(`/Chat_Profiles/{user_id}/device_token`)
.once('value');
return Promise.all([use_Query,deviceToken]).then(result => {
const userName = result[0].val();
const token_id = result[1].val();
const payload = {
notification: {
title: "Friend Request",
body: userName+" has sent you request",
icon: "default",
click_action : "com.pappu5.navigation_TARGET_NOTIFICATION"
},
data : {
from_user_id: from_user
}
};
console.log(payload);
return admin.messaging().sendToDevice(token_id,payload).then(response => {
return console.log('This was the notification feature');
});
});
});
});
答案 0 :(得分:4)
在'Chat_Profiles/'+from_user+'/name'
中,from_user
是一个对象。对象的默认toString
返回[object Object]
,这就是为什么您获得"/Chat_Profiles/[object Object]/name"
的原因,而该user_id
被拒绝了。您可能想使用from_user
,而不是 curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo
。