Android FCM向用户发送消息

时间:2017-03-29 10:56:43

标签: android firebase firebase-cloud-messaging

我使用FCM向客户端(android)发送消息,但我将有许多客户端android,我想只向一个设备发送消息而不是每个人。我怎样才能做到这一点?现在当我发送消息时,我发送给所有人

2 个答案:

答案 0 :(得分:1)

  

向每个设备发送消息

当您从客户端应用程序注册FCM时,您将收到该设备的令牌。您可以将其传递到服务器并将其保存在那里。稍后您可以使用该特定设备令牌将FCM消息发送到该设备。

返回FCM令牌的代码片段在下面给出

      import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;


public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

    private static final String TAG = "MyFirebaseIIDService";

    /**
     * Called if InstanceID token is updated. This may occur if the security of
     * the previous token had been compromised. Note that this is called when the InstanceID token
     * is initially generated so this is where you would retrieve the token.
     */
    // [START refresh_token]
    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);

        // If you want to send messages to this application instance or
        // manage this apps subscriptions on the server side, send the
        // Instance ID token to your app server.
        sendRegistrationToServer(refreshedToken);
    }
    // [END refresh_token]

    /**
     * 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) {
        // TODO: Implement this method to send token to your app server.
    }
}

查看官方doc

答案 1 :(得分:1)

如果您有" FCM注册令牌"您可以使用该用户FCM注册令牌向单个设备或用户发送消息的所有用户

相关问题