【问题标题】:Firebase Token Issue for the higher version Lollipop and Marshmallow更高版本 Lollipop 和 Marshmallow 的 Firebase 令牌问题
【发布时间】:2016-07-05 17:04:48
【问题描述】:

我正在将 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);

【问题讨论】:

  • 能否请您也添加 manifest.xml?
  • 您所问的错误是因为两个 API 交叉。
  • 只用一个api(FCM)
  • 我必须删除哪些 api,如果它们交叉,则也不会为较低版本生成令牌密钥,您怎么看?
  • 我用您编辑的清单替换了清单,但得到相同的错误:07-06 00:10:38.021 10684-10684/com.sixmod.com.client I/regToken: FCM Registration Token: null跨度>

标签: android firebase firebase-cloud-messaging


【解决方案1】:

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 。删除这个。这可能会导致无法收到您的注册令牌。

【讨论】:

  • 遇到同样的问题
  • 显示 manifest.xml 文件。您可以编辑添加此文件的答案。
  • 编辑选项就在问题的下方。
  • 抱歉花了一些时间:我编辑了更改,我没有调用 sendRegistrationToServer() 方法
  • 非常感谢,我也按照您提到的那样在 gradle 中进行了更改,但仍然存在问题
猜你喜欢
  • 1970-01-01
  • 2019-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-15
  • 1970-01-01
相关资源
最近更新 更多