未调用GCM广播接收器onReciever

Mj1*_*992 2 android google-cloud-messaging

我正在尝试为我的Android应用程序实现通知,但我没有OnReceive调用MyBroadcastReciever的事件.

这是我的清单文件:

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<permission android:name="com.example.gcm.permission.C2D_MESSAGE" 
    android:protectionLevel="signature" />
<uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.gcmtester.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver
        android:name="com.example.gcm.MyBroadcastReceiver"
        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="com.example.gcm" />
        </intent-filter>
    </receiver>
    <service android:name="com.example.gcm.MyIntentService" />
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)

这是我试图调用该服务的方式.

    Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
    registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0));
    registrationIntent.putExtra("sender", "MY_SENDER_ID");
    startService(registrationIntent);
Run Code Online (Sandbox Code Playgroud)

MyIntentService代码也类似于这里提到的代码.但仍然BroadcastReciever没有调用我的onRecieve方法.我忘了什么问题是什么?

注意: MyIntentService and BroadcastReciever(com.example.gcm)Main Activity包中的单独包装有关.

dru*_*abs 5

以下是使用谷歌提供的gcm.jar库来使用Google云消息服务的方法.

转到SDK Manager> Extras并下载"Google Cloud Messaging for android"库.你可以在android安装文件夹中找到jar文件.安卓>其它>谷歌> GCM> GCM客户端>距离> gcm.jar

在您的项目中包含此jar.

第1步:修改清单文件:

添加此接收器

    <receiver 
        android:name="com.google.android.gcm.GCMBroadcastReceiver"
        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" />
        </intent-filter>
    </receiver>
Run Code Online (Sandbox Code Playgroud)

添加此服务:

<service android:name="com.your.package.GCMIntentService" />
Run Code Online (Sandbox Code Playgroud)

第2步:创建扩展GCMBaseIntentService的GCMIntentService.确保将其放在应用程序的默认包中,如manifest.xml中manifest的package属性中所定义

这就是它的样子

public class GCMIntentService extends GCMBaseIntentService {

@Override
protected void onError(Context arg0, String arg1) {
    // TODO Auto-generated method stub

}

@Override
protected void onMessage(Context arg0, Intent arg1) {
    // TODO Auto-generated method stub

}

@Override
protected void onRegistered(Context arg0, String arg1) {
    // TODO Auto-generated method stub

}

@Override
protected void onUnregistered(Context arg0, String arg1) {
    // TODO Auto-generated method stub

}

}
Run Code Online (Sandbox Code Playgroud)

第3步:注册设备.

GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
GCMRegistrar.register(this, SENDER_ID);
Run Code Online (Sandbox Code Playgroud)

此SENDER_ID是您在注册GCM时获得的发件人ID(或我忘记的项目ID).就是你完成了.现在以您希望的方式覆盖GCMIntentService中的任何方法.可能会记录一些信息并查看.重写方法有明显的名称.与设备注册完成时将调用onRegistered一样,String参数是GCM注册ID.欲了解更多信息,请点击

希望能帮助到你.