GCM客户端无法在Android上接收推送通知

时间:2015-08-09 05:46:41

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

即使success:1,我的Android客户端也无法接收来自GCM服务器的推送通知。 multicast_id":8703499666074543637,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1439080356936445%9bd2607ff9fd7ecd"

我遵循了 tutorial 。它遵循使用GCM的新实现,其中:

  
      
  1. 在应用清单中,将您的GcmBroadcastReceiver替换为“com.google.android.gms.gcm.GcmReceiver”,并将当前扩展IntentService的服务声明替换为新的GcmListenerService
  2.   
  3. 从客户端代码中删除BroadcastReceiver实现
  4.   
  5. 重构当前的IntentService服务实现以使用GcmListenerService
  6.   

我使用此 link 测试我的应用以获取GCM推送通知。

这是我对BroadcastReciever和IntentService的清单的一部分:

<receiver
            android:name="com.google.android.gms.gcm.GcmReceiver"
            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" />
                <category android:name="com.usc.jay.ifff" />
            </intent-filter>
</receiver>
       <service android:name="com.usc.jay.ifff.GCMIntentService"
        android:exported="false" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service> 

我的IntentService:

public class GCMIntentService extends GcmListenerService{
   public static final int MESSAGE_NOTIFICATION_ID = 435345;

    @Override
    public void onMessageReceived(String from, Bundle data) {
        String message = data.getString("message");
        Loge.e("GCM",message);

        sendNotification(message);
    }


    private void sendNotification(String message) {
        int icon = R.mipmap.ic_launcher;
        long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);

        String title = this.getString(R.string.app_name);

        Intent notificationIntent = new Intent(this, MainActivity.class);
        notificationIntent.putExtra("message", message);
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        notification.setLatestEventInfo(this, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        // Play default notification sound
        notification.defaults |= Notification.DEFAULT_SOUND;

        // Vibrate if vibrate is enabled
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(0, notification);

    }

每当我发送一条下游消息时,logcat上都没有显示任何内容,这意味着它没有到达设备上。请帮忙。我尝试检查每个相关的问题,并尝试了答案,但没有任何作用。如果它真的在客户端收到但不成功会更好。我的实施有问题吗?

BTW,我使用Instance Id API通过getToken获取注册ID,而不是注册方法,因为现在根据Google Developers推荐使用它。

我错过了什么吗?请帮忙。

0 个答案:

没有答案
相关问题