GCM未收到注册令牌

时间:2016-10-20 13:46:52

标签: android google-cloud-messaging firebase-cloud-messaging

我根据Google文档开始了最新GCM注册程序的示例。而且我面临着两个非常常见的问题,很可能在stackoverflow中关注这两个问题。

  1. 未收到GCM注册令牌。
  2. 根据文档,我做了:

     InstanceID instanceID = InstanceID.getInstance(this);
     String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
    

    清单权限:

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    
    <permission
        android:name="com.arpaul.gcm_udacity.gcmService.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    
    <uses-permission android:name="com.arpaul.gcm_udacity.gcmService.permission.C2D_MESSAGE" />
    

    在Manifest中的应用:

    <receiver
            android:name="com.google.android.gms.gcm.GcmReceiver"
            android:exported="true"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.arpaul.gcm_udacity.gcmService" />
            </intent-filter>
        </receiver>
        <service
            android:name=".gcmService.ListenerService"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>
        <service
            android:name=".gcmService.GCMInstanceIDListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID" />
            </intent-filter>
        </service>
        <service
            android:name=".gcmService.RegistrationIntentService"
            android:exported="false">
        </service>
    
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    

    Gradle库:

    compile 'com.google.android.gms:play-services:9.0.0'
    compile 'com.google.android.gms:play-services-gcm:9.0.0'
    

    目前的更新版本是9.6.1我猜,但每当我更改并同步它时,

      

    错误:任务':app:processDebugGoogleServices'的执行失败。   请修改版本冲突,方法是更新google-services插件的版本(有关https://bintray.com/android/android-tools/com.google.gms.google-services/的最新版本的信息)或将com.google.android.gms的版本更新为9.0.0。

    我所有的插件都是最新的 甚至添加了谷歌播放服务的元数据,但仍然没有生成。

    1. 由于最新的GCM程序包括Firebase,因此它刚刚在我的带有Android 4.4版本的Dell标签中崩溃。
    2. 我可能错过了Firebase的支持库吗?

      我知道这两点都是重复的问题,但没有一个解决方案适合我。

      请不要急于标记这个重复的问题或关闭它,如果我错过了什么,请帮助我,坚持2天。

      请帮助..

      编辑: 我错过了FCM文档link。它通过Firebase工作,甚至模拟器也接收GCM消息(没想到这么多)。但是现在我的戴尔标签会尝试使用Android 4.4,当我运行任何有谷歌播放服务库的应用程序时,它总会崩溃。

      如果我选择使用我的服务器而不是Firebase用于消息传递目的,那么任何提示都会导致Firebase对免费使用和定价也有一些限制,以便进行扩展。还有其他程序吗?

1 个答案:

答案 0 :(得分:2)

如果我看到你的清单的许可部分是正确的。也许你对服务和接收器有一些问题。这里是您应该添加到清单中的接收器和服务。

我如何仍然添加推送通知权限。

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

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

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

        <receiver
            android:name="com.google.android.gms.gcm.GcmReceiver"
            android:exported="true"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <category android:name="${applicationId}" />
            </intent-filter>
        </receiver>

        <service
            android:name=".gcm.MyGcmListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>

        <service
            android:name=".gcm.MyInstanceIDListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID" />
            </intent-filter>
        </service>

        <service
            android:name=".gcm.GcmRegistrationService"
            android:exported="false" />

你需要处理收件人的结果,如下所示:

这是您的GcmRegistrationService示例:

public class GcmRegistrationService extends IntentService {
    private static final String TAG = "GcmRegistrationService";

    public GcmRegistrationService() {
        super(TAG);
    }

    @Override
    public void onHandleIntent(Intent intent) {
        try {
            InstanceID instanceID = InstanceID.getInstance(this);
            String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
            // Send token to your server.
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

以下是您的示例MyGcmListenerService:

public class MyGcmListenerService extends GcmListenerService {
    @Override
    public void onMessageReceived(String from, Bundle data) {
       // Handle your notification here.
    }
}

如果刷新了gcm令牌,则在调用此函数时,会显示MyInstanceIDListenerService示例。 (再次致电您的注册服务。)

public class MyInstanceIDListenerService extends InstanceIDListenerService {
    @Override
    public void onTokenRefresh() {
        final Intent intent = new Intent(this, GcmRegistrationService.class);
        startService(intent);
    }
}

最后开始服务获取令牌:

final Intent intent = new Intent(this, GcmRegistrationService.class);
startService(intent);

我希望这会帮助你。祝你好运。