1

我在获取刷新令牌在firebase中遇到了问题。我已经通过文档,并完全按照Android的步骤。在我的日志中,我发现Firebase连接成功。不知道为什么我没有获取实例标记。我处于初始阶段,试图在logcat中获取令牌。没有获取刷新令牌firebase android

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.project.application" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="9" 
     android:targetSdkVersion="17" /> 

    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.GET_ACCOUNTS" /> 

    <uses-permission android:name="android.permission.VIBRATE" /> 
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 

    <application 
     android:allowBackup="false" 
     android:icon="@drawable/icon" 
     android:label="@string/app_name" > 
     <activity 
      android:name=".ProjectName" 
      android:configChanges="keyboard|keyboardHidden|orientation|screenSize" 
      android:icon="@drawable/icon" 
      android:label="@string/app_name" 
      android:theme="@android:style/Theme.Light.NoTitleBar"> 

      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
      <meta-data 
       android:name="com.google.android.gms.version" 
       android:value="@integer/google_play_services_version" /> 
     </activity> 

     <service 
      android:name=".MyInstanceIDListenerService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> 
      </intent-filter> 
     </service> 

     <service 
      android:name=".MyFirebaseInstanceIDService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> 
      </intent-filter> 
     </service>  
    </application> 

</manifest> 

    public class MyInstanceIDListenerService extends FirebaseInstanceIdService{  
    private static final String TAG = "MyFirebaseIIDService"; 
    @Override 
    public void onTokenRefresh() { 
     // Get updated InstanceID token. 
     String refreshedToken = FirebaseInstanceId.getInstance().getToken(); 

     Log.d(TAG, "Refreshed token: " + refreshedToken); 
     sendRegistrationToServer(refreshedToken); 

    } 

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

private static final String TAG = "MyFirebaseMsgService"; 
@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    // TODO(developer): Handle FCM messages here. 

    Log.d(TAG, "From: " + remoteMessage.getFrom()); 
    String messageBody = null; 

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

    if (remoteMessage.getNotification() != null) { 
     messageBody = remoteMessage.getNotification().getBody(); 
     Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); 
    } 

    sendNotification(messageBody); 

} 
private void sendNotification(String messageBody) { 
    Intent intent = new Intent(this, SplashActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
      PendingIntent.FLAG_ONE_SHOT); 

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.ic_stat_ic_notification) 
      .setContentTitle("FCM Message") 
      .setContentText(messageBody) 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 

    NotificationManager notificationManager = 
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
} 

}

+0

令牌只生成一次。您需要清除您的应用数据才能获得新的推送令牌。 – ZeroOne

+0

如果我已经正确理解你,我还没有一次。我遵循了firebase andriod中的步骤,并且我期待log中的令牌。但是我根本无法看到这个Log.d(标记,“刷新标记:”+ refreshedToken)。我应该至少用“刷新令牌”记录日志。但是在日志中没有记录。 – jenny

+0

是否用'startService(新的Intent(this,MyFirebaseMessagingService .class))启动服务;' –

回答

6

onTokenRefresh()方法不会在第一次安装应用程序时触发。它仅由specific scenarios触发。

要获得令牌,必须在应用程序的开始时调用FirebaseInstanceId.getInstance().getToken()(如onCreate或其他)。

+0

如果我在创建,我得到令牌。但仍然没有从Firebase控制台获取通知消息。 – jenny

+0

@jenny你可以发布你的Android端代码来接收消息吗? –

+0

我已更新我的消息服务代码。尽管我登录日志,但仍然无法在移动设备上收到通知。 – jenny

1

解决您的许可

<permission android:name="${applicationId}.permission.C2D_MESSAGE" android:protectionLevel="signature" /> 
<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" /> 

请确保您有正确的谷歌,service.json。

注意:令牌只生成一次。尝试清理应用程序的数据,以获得新令牌

编辑:

或者你可以做迭代令牌手动让您在活动/片段类

FirebaseInstanceId iid = FirebaseInstanceId.getInstance(); 
String token = iid.getToken(); 

while(token == null){ 
    iid = FirebaseInstanceId.getInstance(); 
    token = iid.getToken(); 
} 

Logger.info("TOKEN","==>"+token); 
1

FCMhere

GitHub的项目

文档onTokenRefresh()为:

  • 如果InstanceID令牌已更新,则调用。如果以前令牌的安全性受到影响,则可能会发生这种情况。请注意,当最初生成InstanceID标记时会调用此标记,因此这是您要检索该标记的位置。

你要的,你Clear Cache应用程序或重新安装,以便调用此方法。

0

尝试删除google-services.json文件并重新添加它。