Firebase令牌版本为更高版本的Lollipop和Marshmallow

时间:2016-07-05 17:04:49

标签: android firebase firebase-cloud-messaging

我正在将firebase集成到我的应用程序,但问题是我在棒棒糖下方收到令牌,不知道为什么令牌为棒棒糖而来。这是我的代码。

清单文件

<?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 amitranjan@sixmod5 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);

1 个答案:

答案 0 :(得分:0)

1)只需使用Firebase Cloud Messaging(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。)

不要使用GCM,因为它只是FCM的旧版本。因此,仅使用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 amitranjan@sixmod5 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。删除它。它可能导致您的注册令牌无法被接收。