android - GCMRegistrar checkManifest 使我的应用程序崩溃

标签 android push-notification google-cloud-messaging

我在使用 GCMRegistrar 时遇到问题,这是代码:

// Make sure the device has the proper dependencies.
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this); <- this line is the problem (I suppose)
regId = GCMRegistrar.getRegistrationId(this);

当我将该行更改为评论时,应用程序不会崩溃,但 regId 为空,当我尝试使用该行时,它会使我的应用程序崩溃,我不知道为什么,所以我希望你能帮助我. :)

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.chattest1"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<permission
    android:name="com.example.chattest1.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.example.chattest1.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.chattest1.RegisAct"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.chattest1.Main"
        android:label="@string/title_activity_regis" >
    </activity>
</application>

</manifest>

最佳答案

checkManifest 检查您的 list 是否包含 GCM 工作所需的定义。 当缺少某些内容时,它会抛出 IllegalStateException

在您的情况下,您缺少可以从 GCM 接收消息的广播接收器。

GCMRegistrar.getRegistrationId(this)(返回本地存储的注册 ID)仅当您首先调用 GCMRegistrar.register 时才会返回非空值。请注意,GCMRegistrar.register 是非阻塞的,因此响应不会立即到达。

最后,正如 CommonsWare 所建议的那样,GCMRegistrar 已被弃用,我们鼓励您改用 GoogleClassMessaging 类。

关于android - GCMRegistrar checkManifest 使我的应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17734016/

相关文章:

android - 使用 sencha touch 2 开发的应用程序的推送通知

java - sql查询平均,按毫秒的日期部分分组后

android - Analytics v4 未捕获异常和混淆器

java - HttpURLConnection PUT 到 Google Cloud Storage 给出错误 403

android - 如何以编程方式更改选项卡指示器颜色

android - APNS 是否理解 UTF-8 编码的有效负载

android - GCM 云连接服务器中上游消息传递的主要用途是什么?

android - FCM 主题是否适合更多用户?

java - 通过通用代码向各种设备推送通知

android - Urban Airship Android GCM 未在 Android Oreo 和 Pie 上接收推送通知