简体   繁体   English

使用 FCM 仪表板向特定用户发送通知

[英]Send notification to specific user using FCM Dashboard

Is there any way send notification to user using his email address or UID using the Firebase Cloud Messaging Dashboard (Android app)?有没有办法使用他的 email 地址或使用 Firebase 云消息传递仪表板(Android 应用程序)的 UID 向用户发送通知?

No, for sending push notification you need fcm token.不,要发送推送通知,您需要 fcm 令牌。 Unless you maintain a mapping of userId/email to fcm token it's not possible除非您维护 userId/email 到 fcm 令牌的映射,否则这是不可能的

For php you can use this snipped to send notifications to the users对于 php,您可以使用此片段向用户发送通知

<?php

$tokens = array("yourFcmUserToken");
//you can add more to this array to send the message to multiple users, e.g. fetch an array of tokens from database

$message = array("message" => "This is message from Firebase");
$message_status = send_notification($tokens, $message);
echo "<br>".$message_status;
echo "<br>send: ".$message["message"]."<br>";

function send_notification ($tokens, $message){
    $url = 'https://fcm.googleapis.com/fcm/send';
    $fields = array(
         'registration_ids' => $tokens,
         'data' => $message
        );

    $headers = array(
        'Authorization:key=yourGoogleApiKey',
        'Content-Type: application/json'
        );

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $url);
   curl_setopt($ch, CURLOPT_POST, true);
   curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);  
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
   curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
   $result = curl_exec($ch);
   if ($result === FALSE) {
       die('Curl failed: ' . curl_error($ch));
   }
   curl_close($ch);
   return $result;
}

?>

and receive the message in the app like below并在应用程序中接收消息,如下所示

    public class FirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "FirebaseMessagingService";

    /**
     * Called when message is received.
     *
     * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
     */
    // [START receive_message]
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // [START_EXCLUDE]
        // There are two types of messages data messages and notification messages. Data messages
        // are handled
        // here in onMessageReceived whether the app is in the foreground or background. Data
        // messages are the type
        // traditionally used with GCM. Notification messages are only received here in
        // onMessageReceived when the app
        // is in the foreground. When the app is in the background an automatically generated
        // notification is displayed.
        // When the user taps on the notification they are returned to the app. Messages
        // containing both notification
        // and data payloads are treated as notification messages. The Firebase console always
        // sends notification
        // messages. For more see: firebase.google.com/docs/cloud-messaging/concept-options
        // [END_EXCLUDE]
        
        
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Data: " + remoteMessage.getData());

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());

            if (/* Check if data needs to be processed by long running job */ true) {
                // For long-running tasks (10 seconds or more) use WorkManager.
                
                //todo
                //scheduleJob();
            } else {
                // Handle message within 10 seconds
                //todo
                //handleNow();
            }

        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        }

        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated. See sendNotification method below.
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM