0

我正在尝试在插件中使用 Android 的 GCM 推送通知。我正在从 Google 服务器获取有效的注册 ID。但是当我从服务器发送任何数据时,BroadcastReceiver根本不会调用。我认为问题在于清单中的权限,这是我正在使用的清单部分 -

<receiver
android:name="com.creator.gcm.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.creator.test" />
</intent-filter>
<!-- Receive the registration id -->
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.creator.test" />
</intent-filter>
</receiver>
<service android:name="com.creator.gcm.GcmIntentService" />

<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.VIBRATE"/>


<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>

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

我的 boradcast 接收器代码取自官方 Android 示例,仍然是代码 -

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
    private static final String TAG = "BROADCAST_TAG";
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "onReceive");
        // Explicitly specify that GcmIntentService will handle the intent.
        ComponentName comp = new ComponentName(context.getPackageName(),
                GcmIntentService.class.getName());
        // Start the service, keeping the device awake while it is launching.
        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
    }
}

IntentService -

public class GcmIntentService extends IntentService {

    private static native void native_notificationCallback();

    public static final int NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;
    private static final String TAG = "INTENT_TAG";

    public GcmIntentService() {
        super("GcmIntentService");
        Log.d(TAG, "GcmIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.d(TAG, "onHandleIntent");
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        String messageType = gcm.getMessageType(intent);

        if (!extras.isEmpty()) {
            if (GoogleCloudMessaging.
                    MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                sendNotification("Send error: " + extras.toString());
            } else if (GoogleCloudMessaging.
                    MESSAGE_TYPE_DELETED.equals(messageType)) {
                sendNotification("Deleted messages on server: " +
                        extras.toString());
            // If it's a regular GCM message, do some work.
            } else if (GoogleCloudMessaging.
                    MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                // This loop represents the service doing some work.
                for (int i=0; i<5; i++) {
                    Log.i(TAG, "Working... " + (i+1)
                            + "/5 @ " + SystemClock.elapsedRealtime());
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                    }
                }
                Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                sendNotification("Received: " + extras.toString());
                Log.i(TAG, "Received: " + extras.toString());
            }
        }
        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }
    private void sendNotification(String msg) {
        Log.d(TAG, "Got Notification: "+msg);
        mNotificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, LoaderActivity.class), 0);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
        //.setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("GCM Notification")
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(msg))
        .setContentText(msg);

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }
}

安装时,设备始终在 logcat 生成此消息 -

10-13 21:31:59.329: W/ActivityManager(2190): No content provider found for permission revoke: file:///data/local/tmp/Notification Example.apk
10-13 21:31:59.489: W/ActivityManager(2190): No content provider found for permission revoke: file:///data/local/tmp/Notification Example.apk
10-13 21:32:00.129: I/PackageManager(2190): Running dexopt on: com.creator.test
10-13 21:32:01.371: I/ActivityManager(2190): Force stopping package com.creator.test uid=10180
10-13 21:32:01.531: D/PackageManager(2190): New package installed in /data/app/com.creator.test-1.apk
10-13 21:32:01.531: W/PackageManager(2190): Unknown permission android.permission.ADD_SYSTEM_SERVICE in package com.android.phone
10-13 21:32:01.531: W/PackageManager(2190): Unknown permission com.google.android.providers.gmail.permission.READ_GMAIL in package com.mobisystems.fileman
10-13 21:32:01.541: W/PackageManager(2190): Unknown permission android.permission.RECEIVE_USER_PRESENT in package com.stericsson.atservice
10-13 21:32:01.541: W/PackageManager(2190): Not granting permission android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS to package com.android.browser (protectionLevel=2 flags=0x9be45)
10-13 21:32:01.541: W/PackageManager(2190): Unknown permission com.sonyericsson.android.permission.FACERECOGNITION_SERVICE in package com.sonyericsson.android.camera3d
10-13 21:32:01.541: W/PackageManager(2190): Unknown permission com.android.launcher.permission.READ_SETTINGS in package com.android.settings
10-13 21:32:01.541: W/PackageManager(2190): Unknown permission com.android.launcher.permission.WRITE_SETTINGS in package com.android.settings
10-13 21:32:01.541: W/PackageManager(2190): Unknown permission com.sonyericsson.android.permission.FACERECOGNITION_SERVICE in package com.sonyericsson.android.camera
10-13 21:32:01.541: W/PackageManager(2190): Unknown permission com.google.android.voicesearch.SHORTCUTS_ACCESS in package com.google.android.googlequicksearchbox
10-13 21:32:01.541: W/PackageManager(2190): Unknown permission com.google.android.ears.permission.WRITE in package com.google.android.googlequicksearchbox
10-13 21:32:01.541: W/PackageManager(2190): Unknown permission com.google.android.apps.googlevoice.permission.AUTO_SEND in package com.google.android.googlequicksearchbox
10-13 21:32:01.541: W/PackageManager(2190): Unknown permission com.android.chrome.PRERENDER_URL in package com.google.android.googlequicksearchbox
10-13 21:32:01.541: W/PackageManager(2190): Unknown permission com.facebook.orca.provider.ACCESS in package com.facebook.katana
10-13 21:32:01.541: W/PackageManager(2190): Unknown permission android.permission.ACCESS_LOCATION in package com.sonyericsson.android.servicemenu
10-13 21:32:01.541: W/PackageManager(2190): Unknown permission android.permission.ACCESS_GPS in package com.sonyericsson.android.servicemenu
10-13 21:32:01.541: W/PackageManager(2190): Unknown permission android.permission.ACCESS_WAKE_LOCK in package com.sonyericsson.android.servicemenu
10-13 21:32:01.541: W/PackageManager(2190): Unknown permission com.sonyericsson.servicemenu.permission.ACCESS_SERVICE_MENU in package com.sonyericsson.android.servicemenu
10-13 21:32:01.541: W/PackageManager(2190): Unknown permission com.sony.nfx.app.irremoteservice.permission.EXECUTE_SERVICE in package com.sonyericsson.android.servicemenu
10-13 21:32:01.541: W/PackageManager(2190): Unknown permission android.permission.FM_RADIO_RECEIVER in package com.sonyericsson.android.servicemenu
10-13 21:32:01.541: W/PackageManager(2190): Not granting permission com.sonyericsson.permission.INSTALL_APPLICATION to package com.sonyericsson.trackid (protectionLevel=2 flags=0x8be44)
10-13 21:32:01.541: W/PackageManager(2190): Unknown permission com.sonyericsson.permission.USERAGENT in package com.sonyericsson.trackid
10-13 21:32:01.541: W/PackageManager(2190): Unknown permission com.sonyericsson.permission.CUSTOMIZATION in package com.sonyericsson.trackid
10-13 21:32:01.541: W/PackageManager(2190): Unknown permission com.sonymobile.permission.SOMC_CAMERA in package com.sonymobile.cameraautoupload
10-13 21:32:01.541: W/PackageManager(2190): Unknown permission com.sonyericsson.permission.BLACKLISTED_USB_DEVICE in package com.sonyericsson.extras.liveware
10-13 21:32:01.541: W/PackageManager(2190): Unknown permission com.sonyericsson.music.permission.MUSICSERVICE in package com.sonyericsson.extras.liveware
10-13 21:32:01.551: W/PackageManager(2190): Unknown permission com.sonyericsson.permission.VENDOR_BUTTON in package com.sonyericsson.extras.liveware
10-13 21:32:01.551: W/PackageManager(2190): Not granting permission android.permission.READ_LOGS to package com.stackexchange.marvin (protectionLevel=50 flags=0x8be44)
10-13 21:32:01.551: W/PackageManager(2190): Unknown permission com.android.launcher.permission.READ_SETTINGS in package com.google.android.onetimeinitializer
10-13 21:32:01.551: W/PackageManager(2190): Unknown permission com.android.launcher.permission.WRITE_SETTINGS in package com.google.android.onetimeinitializer
10-13 21:32:01.551: W/PackageManager(2190): Unknown permission com.sonymobile.permission.READ_CONTACT_FACE_META in package com.sonymobile.photoanalyzer
10-13 21:32:01.551: W/PackageManager(2190): Unknown permission com.sonymobile.permission.WRITE_CONTACT_FACE_META in package com.sonymobile.photoanalyzer
10-13 21:32:01.551: W/PackageManager(2190): Not granting permission android.permission.READ_LOGS to package com.avast.android.mobilesecurity (protectionLevel=50 flags=0x28be44)
10-13 21:32:01.551: W/PackageManager(2190): Not granting permission android.permission.MODIFY_PHONE_STATE to package com.avast.android.mobilesecurity (protectionLevel=18 flags=0x28be44)
10-13 21:32:01.551: W/PackageManager(2190): Not granting permission android.permission.DUMP to package com.avast.android.mobilesecurity (protectionLevel=50 flags=0x28be44)
10-13 21:32:01.551: W/PackageManager(2190): Unknown permission android.permission.ACCESS_SUPERUSER in package com.avast.android.mobilesecurity
10-13 21:32:01.551: W/PackageManager(2190): Unknown permission com.sony.walkman.media.service.permission.ACCESS_TEMPORARY_PROVIDER in package com.sonyericsson.video
10-13 21:32:01.551: W/PackageManager(2190): Unknown permission com.sony.walkman.media.service.permission.ACCESS_DRM in package com.sonyericsson.video
10-13 21:32:01.551: W/PackageManager(2190): Unknown permission com.sonyericsson.permission.FMRADIO in package com.sonyericsson.dlna
10-13 21:32:01.551: W/PackageManager(2190): Unknown permission com.google.android.gallery3d.permission.PICASA_STORE in package com.google.android.apps.plus
10-13 21:32:01.551: W/PackageManager(2190): Unknown permission com.google.android.hangouts.START_HANGOUT in package com.google.android.apps.plus
10-13 21:32:01.551: W/PackageManager(2190): Not granting permission android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS to package com.android.vending (protectionLevel=2 flags=0xabec5)
10-13 21:32:01.551: W/PackageManager(2190): Unknown permission android.permission.MANAGE_USERS in package com.android.vending
10-13 21:32:01.551: W/PackageManager(2190): Unknown permission android.permission.INTERACT_ACROSS_USERS in package com.android.vending
10-13 21:32:01.551: W/PackageManager(2190): Not granting permission android.permission.WRITE_SECURE_SETTINGS to package com.android.vending (protectionLevel=50 flags=0xabec5)
10-13 21:32:01.561: W/PackageManager(2190): Unknown permission com.sonyericsson.permission.CRASHSMS in package com.sonyericsson.crashmonitor
10-13 21:32:01.561: W/PackageManager(2190): Unknown permission com.sonymobile.dlna.permission.READ_DATA in package com.sonyericsson.album
10-13 21:32:01.561: W/PackageManager(2190): Unknown permission com.sonyericsson.extras.liveview.permission.LIVEVIEW_API in package com.sonyericsson.androidapp.timescapefeedsplugin
10-13 21:32:01.561: W/PackageManager(2190): Unknown permission com.sonyericsson.permission.CUSTOMIZATION in package com.sonyericsson.androidapp.retaildemo
10-13 21:32:01.561: W/PackageManager(2190): Not granting permission android.permission.CLEAR_APP_USER_DATA to package com.sonyericsson.androidapp.retaildemo (protectionLevel=2 flags=0xbe45)
10-13 21:32:01.561: W/PackageManager(2190): Unknown permission com.sony.walkman.database.media.permission.WRITE_MEDIA_METADATA in package com.sonyericsson.metadatacleanup
10-13 21:32:01.561: W/PackageManager(2190): Unknown permission com.sony.walkman.database.media.permission.ACCESS_DRM_METADATA in package com.sonyericsson.metadatacleanup
10-13 21:32:01.561: W/PackageManager(2190): Unknown permission com.sony.walkman.permission.ACCESS_MEDIA in package com.sonyericsson.metadatacleanup
10-13 21:32:01.561: W/PackageManager(2190): Unknown permission com.timhoeck.android.eventghost.permission.SEND_EVENTS in package de.gavitec.semc
10-13 21:32:01.561: W/PackageManager(2190): Unknown permission com.sonyericsson.permission.CUSTOMIZATION in package com.sonyericsson.textinput.chinese
10-13 21:32:01.561: W/PackageManager(2190): Unknown permission com.android.vending.billing.IBillingAccountService.BIND2 in package com.google.android.gsf.login
10-13 21:32:01.561: W/PackageManager(2190): Unknown permission com.sonyericsson.permission.CONTACTS_PICK_CONVERSATIONS_TARGET in package com.sonyericsson.conversations
10-13 21:32:01.561: W/PackageManager(2190): Unknown permission com.sonyericsson.pws.permission.EMERGENCY_ALERTS in package com.sonyericsson.conversations
10-13 21:32:01.561: W/PackageManager(2190): Unknown permission com.sonyericsson.permission.ACCESS_PROTECTED_CONTACTS in package com.sonyericsson.conversations
10-13 21:32:01.561: W/PackageManager(2190): Unknown permission com.sonyericsson.permission.CUSTOMIZATION in package com.sonyericsson.textinput.uxp
10-13 21:32:01.561: W/PackageManager(2190): Unknown permission android.permission.INTERACT_ACROSS_USERS in package com.google.android.gms
10-13 21:32:01.571: W/PackageManager(2190): Unknown permission android.permission.LOCATION_HARDWARE in package com.google.android.gms
10-13 21:32:01.571: W/PackageManager(2190): Unknown permission android.permission.GET_APP_OPS_STATS in package com.google.android.gms
10-13 21:32:01.571: W/PackageManager(2190): Unknown permission android.permission.UPDATE_APP_OPS_STATS in package com.google.android.gms
10-13 21:32:01.671: W/PackageManager(2190): Unknown permission com.sonyericsson.permission.FMRADIO in package com.sonyericsson.music
10-13 21:32:01.671: W/PackageManager(2190): Not granting permission com.google.android.gm.permission.READ_GMAIL to package com.sonyericsson.music (protectionLevel=2 flags=0x8be45)
10-13 21:32:01.671: W/PackageManager(2190): Unknown permission com.sony.walkman.database.media.permission.WRITE_MEDIA_METADATA in package com.sonyericsson.music
10-13 21:32:01.671: W/PackageManager(2190): Unknown permission com.sony.walkman.database.media.permission.ACCESS_DRM_METADATA in package com.sonyericsson.music
10-13 21:32:01.671: W/PackageManager(2190): Unknown permission com.sony.walkman.media.service.permission.ACCESS_TEMPORARY_PROVIDER in package com.sonyericsson.music
10-13 21:32:01.671: W/PackageManager(2190): Unknown permission com.sonymobile.dlna.permission.READ_DATA in package com.sonyericsson.music
10-13 21:32:01.671: W/PackageManager(2190): Not granting permission android.permission.BIND_DEVICE_ADMIN to package com.android.email (protectionLevel=2 flags=0xbbc45)
10-13 21:32:01.681: W/PackageManager(2190): Unknown permission com.sonyericsson.permission.CUSTOMIZATION in package com.sonyericsson.android.pobox
10-13 21:32:01.681: W/PackageManager(2190): Unknown permission com.android.launcher.permission.PRELOAD_WORKSPACE in package com.google.android.partnersetup
10-13 21:32:01.681: W/PackageManager(2190): Not granting permission android.permission.DEVICE_POWER to package com.sonyericsson.android.socialphonebook (protectionLevel=2 flags=0x8be45)
10-13 21:32:01.681: W/PackageManager(2190): Not granting permission android.permission.ACCESS_DRM to package com.sonyericsson.android.socialphonebook (protectionLevel=2 flags=0x8be45)
10-13 21:32:01.681: W/PackageManager(2190): Unknown permission com.sonyericsson.phone.CALL_SCREEN_SHORTCUT in package com.sonyericsson.android.socialphonebook
10-13 21:32:01.681: W/PackageManager(2190): Unknown permission com.android.email.permission.ACCESS_GAL_PROVIDER in package com.sonyericsson.android.socialphonebook
10-13 21:32:01.681: W/PackageManager(2190): Unknown permission com.sonyericsson.aab.READ_ICE_CONTACTS in package com.sonyericsson.android.socialphonebook
10-13 21:32:01.681: W/PackageManager(2190): Unknown permission com.sonyericsson.permission.CONTACTS_PICK_CONVERSATIONS_TARGET in package com.sonyericsson.android.socialphonebook
10-13 21:32:01.681: W/PackageManager(2190): Not granting permission com.sonyericsson.permission.IDD to package com.sonyericsson.android.socialphonebook (protectionLevel=2 flags=0x8be45)
10-13 21:32:01.681: W/PackageManager(2190): Unknown permission com.google.android.providers.gmail.permission.READ_GMAIL in package com.mobisystems.office
10-13 21:32:01.681: W/PackageManager(2190): Not granting permission android.permission.WRITE_MEDIA_STORAGE to package com.mobisystems.office (protectionLevel=18 flags=0x1cbe44)
10-13 21:32:01.681: W/PackageManager(2190): Not granting permission android.permission.WRITE_SECURE_SETTINGS to package com.whatsapp (protectionLevel=50 flags=0xbe44)
10-13 21:32:01.691: W/PackageManager(2190): Not granting permission android.permission.BROADCAST_SMS to package com.sonyericsson.nfc (protectionLevel=2 flags=0x8be45)
10-13 21:32:01.691: W/PackageManager(2190): Unknown permission com.sonyericsson.permission.CONTACTS_PICK_CONVERSATIONS_TARGET in package com.sonyericsson.nfc
10-13 21:32:01.691: W/PackageManager(2190): Not granting permission android.permission.ACCESS_BLUETOOTH_SHARE to package com.sonyericsson.nfc (protectionLevel=2 flags=0x8be45)
10-13 21:32:01.691: W/PackageManager(2190): Unknown permission com.sonyericsson.permission.ACCESS_PROTECTED_CONTACTS in package com.sonyericsson.nfc
10-13 21:32:01.691: W/PackageManager(2190): Unknown permission android.provider.Telephony.SMS_RECEIVED in package com.sonyericsson.nfc
10-13 21:32:01.691: W/PackageManager(2190): Not granting permission com.google.android.gm.permission.READ_GMAIL to package com.sonyericsson.nfc (protectionLevel=2 flags=0x8be45)
10-13 21:32:01.691: W/PackageManager(2190): Unknown permission com.vodafone.vodafone360updates.permission.SHOW_SETUP_WIZARD in package com.sonyericsson.setupwizard
10-13 21:32:01.691: W/PackageManager(2190): Unknown permission com.sonyericsson.timescape.permission.TIMESCAPE_SETTINGS in package com.sonyericsson.settings
10-13 21:32:01.691: W/PackageManager(2190): Unknown permission com.sonyericsson.eventstream.twitterplugin.AUTHENTICATION in package com.sonyericsson.eventstream.twitterpostviewer
10-13 21:32:01.701: W/PackageManager(2190): Not granting permission android.permission.DEVICE_POWER to package com.sonyericsson.organizer (protectionLevel=2 flags=0x8bc45)
10-13 21:32:01.701: W/PackageManager(2190): Unknown permission com.sony.walkman.database.media.permission.ACCESS_DRM_METADATA in package com.sonyericsson.musicvisualizer
10-13 21:32:01.701: W/PackageManager(2190): Unknown permission andriod.permission.MANAGE_USB in package com.sonyericsson.usbux

我什至不使用大多数这些权限。如果这很重要,我正在使用 Sony Xperia P 进行调试。当我调用使用 GCM 服务器注册我的设备时,这是我在 logcat 中得到的输出 -

10-13 21:33:21.509: W/GCM(2527): DIR: /data/data/com.google.android.gms/app_APP /data/data/com.google.android.gsf
10-13 21:33:21.959: D/BROADCAST_TAG(5204): onReceive
10-13 21:33:21.969: D/INTENT_TAG(5204): GcmIntentService
10-13 21:33:21.979: D/INTENT_TAG(5204): onHandleIntent
10-13 21:35:36.611: I/Icing.InternalIcingCorporaProvider(3573): Updating corpora: A: MAYBE, C: FORCE_ALL
10-13 21:35:38.653: W/ActivityManager(2190): Permission denied: checkComponentPermission() owningUid=10089
10-13 21:35:38.653: W/ActivityManager(2190): Permission denied: checkComponentPermission() owningUid=10089
10-13 21:35:38.693: W/ActivityManager(2190): Permission denied: checkComponentPermission() owningUid=10089
10-13 21:35:38.693: W/ActivityManager(2190): Permission denied: checkComponentPermission() owningUid=10089
10-13 21:35:38.703: W/ActivityManager(2190): Permission denied: checkComponentPermission() owningUid=10089
10-13 21:35:38.703: W/ActivityManager(2190): Permission denied: checkComponentPermission() owningUid=10089
10-13 21:35:38.723: W/ActivityManager(2190): Permission denied: checkComponentPermission() owningUid=10089
10-13 21:35:38.723: W/ActivityManager(2190): Permission denied: checkComponentPermission() owningUid=10089
10-13 21:35:38.753: W/ActivityManager(2190): Permission denied: checkComponentPermission() owningUid=10089
10-13 21:35:38.753: W/ActivityManager(2190): Permission denied: checkComponentPermission() owningUid=10089

所以根据输出,我猜这是某种权限问题。我使用的发件人 ID 和浏览器密钥是正确的,并且我已经验证了很多次。
这里要注意的一件有趣的事情是,当我向 GCM 注册设备时,会调用 broadcastIntent,但不知道为什么,然后根本没有被调用。

我已经检查了很多关于 SO 本身的答案,但没有一个能帮助我解决这个问题。我很感激任何帮助来解决这个问题,我在过去的两周里一直在尝试解决它。

PS:如果需要更多信息来理解问题,请告诉我。

更新:- 当第一次调用 BroadcastReceiver 时,它返回注册 ID。extras.toString()方法返回了这个-

Bundle[{registration_id=****my_device_reg_id**, android.support.content.wakelockid=1}]
4

1 回答 1

1

如果您的主包是“ com.creator.test ”,则将您的 GCMIntentService 类放入该包中并<service android:name=".GcmIntentService" />在清单文件中声明。

于 2013-10-14T06:12:01.460 回答