在gcm接收器中未收到推送

时间:2016-08-10 06:04:45

标签: android push-notification google-cloud-messaging

我知道这个问题已被问到很多。我看了很多答案,但我的问题没有解决。我正在分享我的代码。问题是,当我启动设备时,接收器被执行(当我在我的模拟器中使用nexus 4时)。但是当我连接手机lenovo vibe k5 plus时,启动应用程序时它不会触发接收器。

现在当我从解析发送推送时,手机和模拟器中都没有收到推送。我对发送推送知之甚少。我一周以来一直在努力..

我的清单:

 <receiver
        android:name="com.bison.d2d1.GcmBroadcastReceiver"
        android:exported="true"
        android:permission="com.google.android.c2dm.permission.RECEIVE" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="com.bison.d2d1" />
        </intent-filter>
    </receiver>


    <!-- Register Service -->

    <service android:name="com.bison.d2d1.GCMNotificationIntentService"
        android:enabled="true" >
    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
    </intent-filter>
    </service>

    

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

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

接收器:

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        ComponentName comp = new ComponentName(context.getPackageName(),
                GCMNotificationIntentService.class.getName());
        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);





    }

意图服务:

 public class GCMNotificationIntentService extends IntentService {
        // Sets an ID for the notification, so it can be updated
        public static final int notifyID = 9001;
        NotificationCompat.Builder builder;

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

        @Override
        protected void onHandleIntent(Intent intent) {
            try {

    //            InstanceID instanceID = InstanceID.getInstance(this);
    //            String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
    //                    GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

                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());
                    } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
                            .equals(messageType)) {
                        sendNotification("Message Received from Google GCM Server:nn"
                                +"");

                    }

                }
                else{
                    Toast.makeText(GCMNotificationIntentService.this, "messageType is empty", Toast.LENGTH_SHORT).show();
                }
                GcmBroadcastReceiver.completeWakefulIntent(intent);

            }catch (Exception e){
                Log.e("error",e.getMessage());
            }
        }

        private void sendNotification(String msg) {

            Intent resultIntent = new Intent(this, BoxedHome.class);
            resultIntent.putExtra("msg", msg);
            PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0,
                    resultIntent, PendingIntent.FLAG_ONE_SHOT);

            NotificationCompat.Builder mNotifyBuilder;
            NotificationManager mNotificationManager;

            mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            mNotifyBuilder = new NotificationCompat.Builder(this)
                    .setContentTitle("D2D")
                    .setContentText(msg)
                    .setSmallIcon(R.drawable.ic_launcher);
            // Set pending intent
            mNotifyBuilder.setContentIntent(resultPendingIntent);

            // Set Vibrate, Sound and Light
            int defaults = 0;
            defaults = defaults | Notification.DEFAULT_LIGHTS;
            defaults = defaults | Notification.DEFAULT_VIBRATE;
            defaults = defaults | Notification.DEFAULT_SOUND;

            mNotifyBuilder.setDefaults(defaults);
            // Set the content for Notification
    //        mNotifyBuilder.setContentText("New message from Server");
            // Set autocancel
            mNotifyBuilder.setAutoCancel(true);
            // Post a notification
            mNotificationManager.notify(notifyID, mNotifyBuilder.build());
        }
    }

我将不胜感激任何帮助。我一直在苦苦挣扎。

0 个答案:

没有答案