未调用GCM广播接收器onReciever [英] GCM Broadcast Receiver onReciever is not called

查看:96
本文介绍了未调用GCM广播接收器onReciever的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to implement notifications for my android application but I am not getting OnReceive event of MyBroadcastReciever being called.

这是我的清单文件:

<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>

这就是我试图致电该服务的方式.

Here's how I am trying to call the service.

    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);

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

The MyIntentService code is also similar to the one mentioned here. But still my BroadcastReciever onRecieve method is not being called. What is the problem am I forgetting something ?

注意:MyIntentService and BroadcastReciever类与Main Activity包位于单独的包(com.example.gcm)中.

Note: MyIntentService and BroadcastReciever classes are in a seperate package(com.example.gcm) from the Main Activity package.

推荐答案

在这里,您可以使用Google提供的gcm.jar库来使用Google的云消息传递服务.

Here is how you can use Google's cloud messaging service using the gcm.jar library that google provides.

转到SDK管理器>其他,然后下载适用于Android的Google Cloud Messaging"库.您可以在android安装文件夹中找到jar文件. Android> extras> google> gcm> gcm-client> dist> gcm.jar

Go to SDK Manager>Extras and download "Google Cloud Messaging for android" library. You can find the jar file in android installation folder. Android>extras>google>gcm>gcm-client>dist>gcm.jar

将此jar包含在您的项目中.

Include this jar in your project.

第1步:修改清单文件:

Step 1: modify your manifest file:

添加此接收者

    <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>

添加此服务:

<service android:name="com.your.package.GCMIntentService" />

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

Step 2: Create GCMIntentService extending GCMBaseIntentService. Make sure you put it in your app's default package as defined in the manifest's package attribute in manifest.xml

这是它的样子

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

}

}

第3步:注册设备.

GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
GCMRegistrar.register(this, SENDER_ID);

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

this SENDER_ID is the sender ID (or project ID i forgot) you got while registering for GCM. That's it you are done. Now override any method in GCMIntentService the way you want. May be log some information and see. The overriden methods have obvious names. Like onRegistered will be called when device registration is complete the String argument is the GCM registration ID. for more info click here

希望有帮助.

这篇关于未调用GCM广播接收器onReciever的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆