我根据 Google 文档为最新的 GCM 注册程序启动了一个示例。我面临两个非常常见的问题,确实查看了 stackoverflow 中关于这两个问题的所有帖子。
- 未收到 GCM 注册 token 。
根据文档,我做了:
InstanceID instanceID = InstanceID.getInstance(this);
String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
list 权限:
<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" />
list 中的应用:
<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" />
渐变库:
compile 'com.google.android.gms:play-services:9.0.0'
compile 'com.google.android.gms:play-services-gcm:9.0.0'
我猜当前更新的版本是 9.6.1,但每当我更改和同步它时它都会射击
Error:Execution failed for task ':app:processDebugGoogleServices'. Please fix the version conflict either by updating the version of the google-services plugin (information about the latest version is available at https://bintray.com/android/android-tools/com.google.gms.google-services/) or updating the version of com.google.android.gms to 9.0.0.
我所有的插件都是最新的 甚至为 Google Play 服务添加了元数据,但仍然没有生成 token 。
- 由于最新的 GCM 程序包括 Firebase,它在我的 Android 4.4 版戴尔选项卡中崩溃了。
有没有我可能遗漏的 Firebase 支持库?
我知道这两个问题都是重复的问题,但没有一个解决方案对我有用。
请不要急于标记这个重复的问题或关闭它,如果我遗漏了任何东西,请帮助我,卡住了 2 天。
请帮忙..
编辑: 我错过了 FCM 文档 link .它通过 Firebase 工作,甚至模拟器也在接收 GCM 消息(没想到这么多)。但是会在我的戴尔标签中试用现在有 Android 4.4,当我运行任何有 google-play-service 库的应用程序时它总是崩溃。
任何提示,如果我选择使用我的服务器而不是 Firebase 来进行消息传递,因为 Firebase 对免费使用和扩展定价也有一些限制。还有其他手续吗?
最佳答案
如果我没看错,您的 list 的许可部分是正确的。也许您对服务和接收器有一些疑问。以下是您应该添加到 list 中的接收器和服务。
我如何添加推送通知权限。
<!-- 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.
}
}
这是您的 MyInstanceIDListenerService 示例,当您的 gcm token 被刷新时被调用。 (再次调用您的注册服务。)
public class MyInstanceIDListenerService extends InstanceIDListenerService {
@Override
public void onTokenRefresh() {
final Intent intent = new Intent(this, GcmRegistrationService.class);
startService(intent);
}
}
最后启动服务获取token:
final Intent intent = new Intent(this, GcmRegistrationService.class);
startService(intent);
希望对您有所帮助。祝你好运。
关于android - GCM 未收到注册 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40156294/