使用Firebase获取GCM注册ID [英] Getting GCM Registration ID using Firebase

查看:131
本文介绍了使用Firebase获取GCM注册ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.net MVC服务器,我想把推送通知发送到我的Android应用程序。我已经在应用中实施了 Firebase Messaging ,并且从Firebase信息中心发送的通知正常工作。

我已经尝试通过发送一个post请求来发送推送通知,但是请求需要一个字段。在此之前,我们曾经发送过由 GCM 提供的注册ID 。现在,由于Firebase正在处理它,我怎样才能在Android中使用Firebase SDK将注册ID提取到字段中?

解决方案

<您好!感谢您使用Firebase Cloud Messaging!



您可以获取注册ID:

  FirebaseInstanceId.getInstance()为gettoken(); 

与GCM sdk不同的是,新的库会自动尽快的取得令牌,然后将它缓存到本地。



上面的方法将返回标记,如果可用,或者返回 null 抓取阶段仍在进行中。

您可以使用回调 onTokenRefresh()可用或已被轮换。

$ pre $ public $ InstanceIDService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh( ){
String refreshedToken = FirebaseInstanceId.getInstance()。getToken();
Log.d(TAG,Refreshed token:+ refreshedToken);
sendRegistrationToMyServer(refreshedToken);




$ b $ p
$ b $ p $更多信息:https://firebase.google.com/docs/cloud-messaging/android/client#sample-register

I have an ASP.net MVC server from where I want to send push notifications to my Android App. I have already implemented Firebase Messaging in the app and the notifications are working fine when sent from Firebase dashboard.

I have tried sending push notification using the server by sending a post request, but the request requires a to field. Earlier we used to send registration id provided by GCM there. Now since Firebase is handling it, how can I fetch the registration id to be put in the to field using Firebase SDK in Android?

解决方案

Hi and thanks for using Firebase Cloud Messaging!

You can fetch the registration-id calling:

FirebaseInstanceId.getInstance().getToken();

Differently from the GCM sdk, the new library automatically takes care of fetching the token as soon as possible, and then it caches it locally.

The above method will return the token, if available, or null if the fetching phase is still in progress.

You can use the callback onTokenRefresh() to be notified when the token is available or has been rotated.

public class InstanceIDService extends FirebaseInstanceIdService {
    @Override
    public void onTokenRefresh() {
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);
        sendRegistrationToMyServer(refreshedToken);
    }
}

More info here: https://firebase.google.com/docs/cloud-messaging/android/client#sample-register

这篇关于使用Firebase获取GCM注册ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆