我不知道为什么会发生这种情况,但每当我获得DeviceID后安装我的应用程序(对于推送通知),我都会收到一封我从未发送的通知。
有人可以帮我找出导致这种行为的原因吗?
这是我BroadcastReceiver
的代码:
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
}
}
这是我的IntentService
:
public class GcmIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
public GcmIntentService() {
super("mycode"); //I've edited my code
}
@Override
protected void onHandleIntent(Intent intent) {
String message = intent.getExtras().getString("alert");
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, AnimationActivity.class), 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setSound(alarmSound)
.setVibrate(new long[] { 0, 100, 200, 300})
.setContentTitle(message);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
}
为了检索我的DeviceID,我只使用:
InstanceID instanceID = InstanceID.getInstance(AnimationActivity.this);
regID = instanceID.getToken(("mycode"),GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); //I've edited my code
答案 0 :(得分:0)
每当从Google服务器获得注册ID时,都会调用GCMBroadcastRecevier类,以便通过GCMIntentService生成通知。我确信客户端实现的方法是错误的 为方便客户端实现GCM,您可以参考此链接。 https://github.com/codepath/android_guides/wiki/Google-Cloud-Messaging