0

我真的被卡住了,并且只想将通知只发送给使用Firebase的特定用户。我知道FCM方法并获得了令牌。但是本教程或其他教程使我向包括发件人在内的两个用户发送推送通知。我只想收到收件人设备上的通知。这是漫长的一天,我一直在解决这个问题。 现在寻找收件人的某个键或UID。它是从我有的令牌获取:FirebaseInstanceId.getInstance().getToken();需要将通知只发送给收件人的设备,而不是发件人的发件人

更新:我无法存储和检索令牌(设备令牌)和其他子项,也能够在Firebase“函数”选项卡中记录消息,但仍然无法将通知推送到设备。我正在仿真器上测试它。我应该在真实设备上测试它吗?

我的Node.js代码:

const functions = require('firebase-functions'); 
const admin = require('firebase-admin'); 
admin.initializeApp(functions.config().firebase); 

var rootRef = 
functions.database.ref('/notifications/{user_id}/{notification_id}'); 

    exports.sendNotification = rootRef.onWrite(event => { 


    const user_id = event.params.user_id; 
    const notification_id = event.params.notification_id; 

    var request = event.data.val(); 

    var payload = { 
    data: { 
     title : "New Message from ", 
     body: "userName has sent you message", 
     icon: "default", 
    } 
    }; 

console.log('We have a notification for device token: ', user_id); 
console.log('Checking if getting inside the node -- ', request.user); 


admin.messaging().sendToDevice(user_id, payload) 
.then(function(response){ 
    console.log('This was the notification Feature',response);  
}) 
.catch(function(error){ 
    console.log('Error sending message ',error);  

    }) 

}) 

MyFirebaseMEssagingService.java

public class MyFirebaseMessagingService extends FirebaseMessagingService 
{ 
    private static final String TAG = "FCM Service"; 
    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     // TODO: Handle FCM messages here. 
     // If the application is in the foreground handle both data and 
     notification messages here. 
     // Also if you intend on generating your own notifications as a result 
     of a received FCM 
     // message, here is where that should be initiated. 
     //remoteMessage.getNotification().getBody()); 

     if(remoteMessage.getNotification()!=null){ 
      Map<String,String> payload = remoteMessage.getData(); 
      //   String title = 
      remoteMessage.getNotification().getTitle(); 
      //   String message = 
      remoteMessage.getNotification().getBody(); 
      //  Log.d(TAG, "Message Notification Title : " + title); 
      //  Log.d(TAG, "Message Notification Body: " + message); 
      System.out.println("Messaging Service : Title - " + payload.get("title") 
      + " , body - " + payload.get("body")); 
      sendNotification(payload); 

     } 
    } 

    @Override 
    public void onDeletedMessages(){ 

    } 

    private void sendNotification(Map<String,String> payload){ 

     NotificationCompat.Builder builder = new 
       NotificationCompat.Builder(this); 
     builder.setSmallIcon(R.mipmap.ic_launcher); 
     builder.setContentTitle("Firebase Push 
       Notification"+payload.get("title")); 
       builder.setContentText("Notification Text : "+ payload.get("body")); 
       Intent intent = new Intent(this, ChatWindow.class); 
       TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
       stackBuilder.addNextIntent(intent); 
       PendingIntent pendingIntent = 
         stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT); 
       builder.setContentIntent(pendingIntent); 
       NotificationManager notificationManager = (NotificationManager) 
         getSystemService(Context.NOTIFICATION_SERVICE); 
       notificationManager.notify(0, builder.build()); 

    } 

} 

FirebaseIDService.java:

public class FirebaseIDService extends FirebaseInstanceIdService { 
    private static final String TAG = "FirebaseIDService"; 

    @Override 
    public void onTokenRefresh() { 
     // Get updated InstanceID token. 
     String refreshedToken = FirebaseInstanceId.getInstance().getToken(); 
     Log.d(TAG, "Refreshed token: " + refreshedToken); 

     DatabaseReference firebase ;//= new Firebase("https://materialtabs- 
     b5734.firebaseio.com//messages"); 
     firebase = 
     FirebaseDatabase.getInstance().getReferenceFromUrl("https://materialtabs- 
       b5734.firebaseio.com//notifications/"); 

       // TODO: Implement this method to send any registration to your app's 
       servers. 
       sendRegistrationToServer(refreshedToken); 
    } 

    /** 
    * Persist token to third-party servers. 
    * 
    * Modify this method to associate the user's FCM InstanceID token with any 
    server-side account 
    * maintained by your application. 
    * 
    * @param token The new token. 
    */ 
    private void sendRegistrationToServer(String token) { 
     // Add custom implementation, as needed. 
     System.out.println("Reading Token : "+ token); 
    } 
} 

而且我开始我的 'MyFirebaseMessagingService.java' 在我的MainActivity文件在“onCreate()” 作为“startService(new I ntent(this,MyFirebaseMessagingService.class));“

请帮帮我。我想我错过了一些小的细节,或者它可能是重大的,不确定!

+1

请显示您的代码。 – timiTao

+0

嘿!我更新了我的问题。你可以看看它。谢谢 ! –

回答

1

为此,您需要存储收件人设备的firebase令牌,然后每当您要将通知发送给收件人时,都必须从数据库中检索收件人的令牌,然后可以使用该令牌给唯一的收件人。

+0

嗨,我将令牌理解为数据库的设备令牌,并且能够在Firebase中获取控制台消息和令牌,但仍无法将通知推送到设备。 我在模拟器上测试我的代码,我应该在真实设备上测试它吗? 我的node.js代码: –

+0

是的,你需要在真实的设备上测试。我很抱歉迟到的回复。 –

相关问题