Java 谷歌云消息接收方意图未启动(广播意图回调:result=CANCELLED forIntent)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19090058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:59:46  来源:igfitidea点击:

Google cloud messaging receiver intent does not start (broadcast intent callback: result=CANCELLED forIntent)

javaandroidandroid-intentpush-notificationgoogle-cloud-messaging

提问by

I am trying to make a GCM client, registration is fine. I am also successfully sending messages from server. However, the client does not start the intent. It says

我正在尝试制作 GCM 客户端,注册没问题。我也成功地从服务器发送消息。但是,客户端不会启动意图。它说

09-30 08:39:59.795: W/GTalkService(4667): [DataMsgMgr] broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE cat=[dol.framework] (has extras) }

09-30 08:39:59.795: W/GTalkService(4667): [DataMsgMgr] 广播意图回调:result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE cat=[dol.framework](有额外的)}

My Intent

我的意图

public class GCMService extends IntentService{

    public GCMService(String name) {
        super("GCMService");
    }

    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        String messageType = gcm.getMessageType(intent);
        android.util.Log.i("hi","test");
        if (!extras.isEmpty()) {

            if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                Logger.logInUI(tag, "Received: " + extras.toString());
            }
        }
        GCMReceiver.completeWakefulIntent(intent);
    }
}

And my receiver

还有我的接收器

public class GCMReceiver extends WakefulBroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        ComponentName comp = new ComponentName(context.getPackageName(),
                GCMService.class.getName());
        startWakefulService(context, (intent.setComponent(comp)));
        Logger.log("hi","eetett");
        setResultCode(Activity.RESULT_OK);
    }
}

And finally my manifest

最后我的清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="dol.framework"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <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" />

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

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


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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:name="dol.framework.widget.DolApplication"
        android:label="Dol Framework"
        android:theme="@style/AppTheme" >
        <activity
            android:name="dol.framework.activity.DebugActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver
            android:name="dol.framework.GCMReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="dol.framework" />
            </intent-filter>
        </receiver>

        <service android:name="dol.framework.GCMService"/>
    </application>

</manifest>

Sorry for the wall of text. Both GCMService and GCMReceiver is at top of my package (dol.framework). What am I doing wrong? Other than those, I am not doing much. Only registering the device and then I am sending a message to this device. Log prints the message at top but does nothing. Shouldn't I start service or something? I didn't see anything related on examples.

对不起,文字墙。GCMService 和 GCMReceiver 都在我的包 (dol.framework) 的顶部。我究竟做错了什么?除了这些,我没有做太多事情。仅注册设备,然后我向该设备发送消息。Log 在顶部打印消息但什么也不做。我不应该开始服务吗?我没有看到任何与示例相关的内容。

采纳答案by Eran

In the permission you defined and used for GCM you have dol.framework.gcm.permission. It should be dol.framework.permission, since your app's package is dol.framework.

在您为 GCM 定义和使用的权限中,您有 dol.framework.gcm.permission。它应该是 dol.framework.permission,因为您的应用程序包是 dol.framework。

回答by dlee

Try changing the receiver bit of your manifest to this:

尝试将清单的接收器位更改为:

   <receiver
        android:name=".GCMReciever"
        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" />
            <action android:name="dol.framework" />
        </intent-filter>
    </receiver>