Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
接收Android通知_Android_Notifications_Push Notification_Broadcastreceiver_Google Cloud Messaging - Fatal编程技术网

接收Android通知

接收Android通知,android,notifications,push-notification,broadcastreceiver,google-cloud-messaging,Android,Notifications,Push Notification,Broadcastreceiver,Google Cloud Messaging,为了能够收到GCM通知,我遵循了文档。 这是我的舱单: 以及IntentService: public class GcmIntentService extends IntentService { public static final int NOTIFICATION_ID = 1; private NotificationManager mNotificationManager; NotificationCompat.Builder builder;

为了能够收到GCM通知,我遵循了文档。 这是我的舱单:

以及
IntentService

public class GcmIntentService extends IntentService
{
    public static final int     NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder  builder;

    public GcmIntentService()
    {
        super("GcmIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent)
    {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        // The getMessageType() intent parameter must be the intent you received
        // in your BroadcastReceiver.
        String messageType = gcm.getMessageType(intent);

        if (!extras.isEmpty())
        { // has effect of unparcelling Bundle
            /*
             * Filter messages based on message type. Since it is likely that
             * GCM will be extended in the future with new message types, just
             * ignore any message types you're not interested in, or that you
             * don't recognize.
             */
            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++)
                {

                    try
                    {
                        Thread.sleep(5000);
                    } 
                    catch (InterruptedException e)
                    {
                    }
                }
                // Post notification of received message.
                sendNotification("Received: " + extras.toString());
            }
        }
        // Release the wake lock provided by the WakefulBroadcastReceiver.
        Test.completeWakefulIntent(intent);
    }

    // Put the message into a notification and post it.
    // This is just one simple example of what you might choose to do with
    // a GCM message.
    private void sendNotification(String msg)
    {
        mNotificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);

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

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

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }
}
但是,当调试时,我从未进入
gcminentservice
,调试器进入
Test
类,什么也没有发生

我错过了什么?似乎
startWakefulService
没有打开
gcminentService

注意: 我已经按照@Eran的建议更改了代码(仍然不起作用:()

只有当你的GCMinentService类在你的应用程序的主包中时,这才有效,我假设不是这样(因为你的receiver类在一个不同的包中-
com.evapp.notification
,而不是你的应用程序的主包-
com.evapp.activities

您可以尝试以下方法:

Intent gcmIntent = new Intent(context, GcmIntentService.class);
gcmIntent.putExtras(intent.getExtras());
startWakefulService(context, gcmIntent);

仍然没有什么,我是否也应该更改清单?以下是
gcminentt
(tosString())
Intent{cmp=com.evapp.activities/com.evapp.notification.gcminentservice(has extras)}
@vladiffe如果您发布完整的清单,我将能够回答这个问题。@vladiffe应用程序的包名是
com.evapp.activities
。该包名应替换您在
com.example.gcm
@vladiffe中的位置。它还应替换
中的包名。
public class GcmIntentService extends IntentService
{
    public static final int     NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder  builder;

    public GcmIntentService()
    {
        super("GcmIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent)
    {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        // The getMessageType() intent parameter must be the intent you received
        // in your BroadcastReceiver.
        String messageType = gcm.getMessageType(intent);

        if (!extras.isEmpty())
        { // has effect of unparcelling Bundle
            /*
             * Filter messages based on message type. Since it is likely that
             * GCM will be extended in the future with new message types, just
             * ignore any message types you're not interested in, or that you
             * don't recognize.
             */
            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++)
                {

                    try
                    {
                        Thread.sleep(5000);
                    } 
                    catch (InterruptedException e)
                    {
                    }
                }
                // Post notification of received message.
                sendNotification("Received: " + extras.toString());
            }
        }
        // Release the wake lock provided by the WakefulBroadcastReceiver.
        Test.completeWakefulIntent(intent);
    }

    // Put the message into a notification and post it.
    // This is just one simple example of what you might choose to do with
    // a GCM message.
    private void sendNotification(String msg)
    {
        mNotificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);

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

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

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }
}
{"multicast_id":7269872883857375111,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1394655370108962%e210e99800364492"}]}
ComponentName comp = new ComponentName(context.getPackageName(),
                                       GcmIntentService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
Intent gcmIntent = new Intent(context, GcmIntentService.class);
gcmIntent.putExtras(intent.getExtras());
startWakefulService(context, gcmIntent);