2016-07-05 45 views
1

//你好大家,我正在整合firebase到我的應用程序,但問題是,我收到令牌下面的lolipop,不知道爲什麼令牌將爲null lolipop.Here是我的代碼。任何幫助讚賞。Firebase令牌問題的更高版本Lolipop和棉花糖

清單文件

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    package="com.sixmod.com.client"> 
    <uses-feature 
     android:glEsVersion="0x00020000" 
     android:required="true" /> 
    <!--GCM [email protected] id--> 
    <!-- AIzaSyBo-v2RUKKoe37pZwYeurtoDFw1egxy7fM--> 
    <uses-permission 
     android:name="android.permission.WRITE_EXTERNAL_STORAGE" 
     android:maxSdkVersion="22" /> 
    <uses-permission 
     android:name="android.permission.READ_EXTERNAL_STORAGE" 
     android:maxSdkVersion="22" /> 
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 
    <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/> 
    <uses-permission android:name="android.permission.GET_TASKS"/> 
    <uses-permission android:name="android.permission.READ_CONTACTS" /> 



    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 


    <uses-permission 
     android:name="com.google.android.c2dm.permission.RECEIVE" /> 
    <permission 
     android:name="com.sixmod.com.client.permission.C2D_MESSAGE" 
     android:protectionLevel="signature" /> 
    <uses-permission 
     android:name="com.sixmod.com.client.permission.C2D_MESSAGE" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/truck" 
     android:label="@string/app_name" 
     android:name="com.sixmod.com.sixmod.MyLocalytic.MyApplication" 

     android:supportsRtl="true" 
     android:theme="@style/AppTheme" 
     tools:replace="android:icon"> 



     <service android:name="com.sixmod.com.sixmod.MyReceiver.BroadcastService" /> 



    <activity 

      android:name="com.sixmod.FragmentContainerActivity" 
      android:windowSoftInputMode="adjustPan" 
     android:screenOrientation="portrait" 
      android:theme="@style/AppTheme.NoActionBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 


     <!--<meta-data 
     android:name="com.google.android.maps.v2.API_KEY" 
     android:value="AIzaSyCsguzmCQrHlTaBQ_6ixFyQUk" />--> 
     <meta-data 
      android:name="com.google.android.maps.v2.API_KEY" 
      android:value="AIzaSyBo-37pZwYeurtoDFw1egxy7fM " /> 


     <meta-data 
      android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 
     <meta-data 
      android:name="com.bugsnag.android.API_KEY" 
      android:value="221c5b778e92fdd345fa63e09"/> 


     <meta-data android:name="LOCALYTICS_APP_KEY" android:value="7f652c5db10aa2dbd542-d159-11e5-6885-002dea3c3994"/>  <receiver 
      android:name="com.sixmod.com.sixmod.MyLocalytic.LocalyticCustomReceiver" 
      android:permission="com.google.android.c2dm.permission.SEND"> 
      <intent-filter> 
       <action android:name="com.google.android.c2dm.intent.REGISTRATION"/> 
       <action android:name="com.google.android.c2dm.intent.RECEIVE"/> 

       <category android:name="com.sixmod.com.sixmod.MyLocalytic"/> 
      </intent-filter> 
     </receiver> 

     <service 
      android:name="com.sixmod.com.sixmod.service.MyOrderService" 
      android:exported="false"> 
     </service> 

     <!--<meta-data 
      android:name="LOCALYTICS_APP_KEY" 
      android:value="YOUR-LOCALYTICS-APP-KEY"/>--> 
     <!-- <receiver 
      android:name="com.localytics.android.ReferralReceiver" 
      android:exported="true"> 
      <intent-filter> 
       <action android:name="com.android.vending.INSTALL_REFERRER" /> 
      </intent-filter> 
     </receiver>--> 

     <intent-filter> 
      <data android:scheme="7f652c5db10aa2dbd5b87f7-80b0ac42-d159-11e5-6885-002dea3c3994" /> 
      <action android:name="android.intent.action.VIEW"/> 
      <category android:name="android.intent.category.DEFAULT"/> 
      <category android:name="android.intent.category.BROWSABLE"/> 
     </intent-filter> 

     <service 
      android:name="com.sixmod.com.sixmod.notificationFireBase.MyFirebaseMessagingService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.MESSAGING_EVENT"/> 
      </intent-filter> 
     </service> 

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

     <service 
      android:name="com.sixmod.com.sixmod.notificationFireBase.RegistrationIntentService"> 

     </service> 


    </application> 

</manifest> 

MyFirebaseInstanceIDService:

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); 

     // TODO: Implement this method to send any registration to your app's servers. 
     // 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) { 
     // Add custom implementation, as needed. 
     OkHttpClient client= new OkHttpClient(); 
     RequestBody body=new FormBody.Builder().add("token", token).build(); 
     Request request=new Request.Builder().url("https://logizoapp-cf159.firebaseio.com/").build();// enter php url 
     try { 
      client.newCall(request).execute(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } 
} 

MyFirebaseMessagingService:

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

    private static final String TAG = "MyFirebaseMsgService"; 

    /** 
    * 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) { 
     // TODO(developer): 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. See sendNotification method below. 
     Log.d(TAG, "From: " + remoteMessage.getFrom()); 
     Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody()); 
     sendNotification(remoteMessage.getNotification().getBody()); 
    } 
    // [END receive_message] 

    /** 
    * Create and show a simple notification containing the received FCM message. 
    * 
    * @param messageBody FCM message body received. 
    */ 
    private void sendNotification(String messageBody) { 
     Intent intent = new Intent(this, FragmentContainerActivity.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()); 
    } 
} 

從片段類

String token = FirebaseInstanceId.getInstance().getToken(); 
Log.i("regToken", "FCM Registration Token: " + token); 
+0

你能否加上manifest.xml呢? – Saini

+0

您所問的錯誤是因爲兩個API橫渡他們的方式。 – Saini

+0

只需使用一個api(FCM) – Saini

回答

0

1)只需使用Firebase雲消息傳遞(FCM)API替換GCM即可。

2)演示項目:https://github.com/firebase/quickstart-android/tree/master/messaging

3)瞭解更多信息:https://firebase.google.com/docs/cloud-messaging/android/client

更新:

你的項目是不正確的。它有很多錯誤的可能性。

這樣做的原因是您正在使用2 API的(GCM和FCM在一個項目。)

,因爲它是唯一的FCM的舊版本,不要使用GCM。因此,只能使用FCM。您在此使用的c2dm函數現在不在FCM版本中。它是舊的。

不過你可以試試下面的清單代碼:只需複製並粘貼

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    package="com.sixmod.com.client"> 
    <uses-feature 
     android:glEsVersion="0x00020000" 
     android:required="true" /> 
    <!--GCM [email protected] id--> 
    <!-- AIzaSyBo-v2RUKKoe37pZwYeurtoDFw1egxy7fM--> 
    <uses-permission 
     android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission 
     android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 
    <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/> 
    <uses-permission android:name="android.permission.GET_TASKS"/> 
    <uses-permission android:name="android.permission.READ_CONTACTS" /> 



    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 



    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/truck" 
     android:label="@string/app_name" 
     android:name="com.sixmod.com.sixmod.MyLocalytic.MyApplication" 

     android:supportsRtl="true" 
     android:theme="@style/AppTheme" 
     tools:replace="android:icon"> 



     <service android:name="com.sixmod.com.sixmod.MyReceiver.BroadcastService" /> 



    <activity 

      android:name="com.sixmod.FragmentContainerActivity" 
      android:windowSoftInputMode="adjustPan" 
     android:screenOrientation="portrait" 
      android:theme="@style/AppTheme.NoActionBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 


     <!--<meta-data 
     android:name="com.google.android.maps.v2.API_KEY" 
     android:value="AIzaSyCsguzmCQrHlTaBQ_6ixFyQUk" />--> 
     <meta-data 
      android:name="com.google.android.maps.v2.API_KEY" 
      android:value="AIzaSyBo-37pZwYeurtoDFw1egxy7fM " /> 


     <meta-data 
      android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 
     <meta-data 
      android:name="com.bugsnag.android.API_KEY" 
      android:value="221c5b778e92fdd345fa63e09"/> 


     <meta-data android:name="LOCALYTICS_APP_KEY" android:value="7f652c5db10aa2dbd542-d159-11e5-6885-002dea3c3994"/>  <receiver 
      android:name="com.sixmod.com.sixmod.MyLocalytic.LocalyticCustomReceiver" 
      android:permission="com.google.android.c2dm.permission.SEND"> 
      <intent-filter> 
       <action android:name="com.google.android.c2dm.intent.REGISTRATION"/> 
       <action android:name="com.google.android.c2dm.intent.RECEIVE"/> 

       <category android:name="com.sixmod.com.sixmod.MyLocalytic"/> 
      </intent-filter> 
     </receiver> 

     <service 
      android:name="com.sixmod.com.sixmod.service.MyOrderService" 
      android:exported="false"> 
     </service> 

     <!--<meta-data 
      android:name="LOCALYTICS_APP_KEY" 
      android:value="YOUR-LOCALYTICS-APP-KEY"/>--> 
     <!-- <receiver 
      android:name="com.localytics.android.ReferralReceiver" 
      android:exported="true"> 
      <intent-filter> 
       <action android:name="com.android.vending.INSTALL_REFERRER" /> 
      </intent-filter> 
     </receiver>--> 

     <intent-filter> 
      <data android:scheme="7f652c5db10aa2dbd5b87f7-80b0ac42-d159-11e5-6885-002dea3c3994" /> 
      <action android:name="android.intent.action.VIEW"/> 
      <category android:name="android.intent.category.DEFAULT"/> 
      <category android:name="android.intent.category.BROWSABLE"/> 
     </intent-filter> 

     <service 
      android:name="com.sixmod.com.sixmod.notificationFireBase.MyFirebaseMessagingService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.MESSAGING_EVENT"/> 
      </intent-filter> 
     </service> 

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


    </application> 

</manifest> 

老答案:

不要使用

> getActivity().startService(new 
> Intent(getActivity(),RegistrationIntentService.class)); 

這是從Manifest文件自動註冊的。你不需要RegistrationIntentService.java。除去這

1)的第一件事是使用:*(For this update the Google Repository and Google Play Service from the Android SDK Manager.)*

compile 'com.google.firebase:firebase-messaging:9.2.0'

2)取消註釋:

sendRegistrationToServer(refreshedToken);

3)你不需要RegistrationIntentService.java。刪除這個。它可能會導致您的註冊令牌不被接收。

+0

謝謝,讓我檢查 –

+0

yup檢查並上傳manifest.xml文件 – Saini

+0

得到同樣的問題 –