Below is my code of client:
public class GCMIntentService extends GCMBaseIntentService {
public GCMIntentService() {
super(SENDER_ID);
}
@Override
protected void onError(Context arg0, String arg1) {
}
@Override
protected void onMessage(Context arg0, Intent arg1) {
Context context = getApplicationContext();
String NotificationContent = arg1.getStringExtra("message");
System.out.println("Message: " + NotificationContent);
}
@Override
protected void onRegistered(Context arg0, String arg1) {
System.out.println("Registered id: " + arg1);
}
@Override
protected void onUnregistered(Context arg0, String arg1) {
System.out.println("Unregistered id: " + arg1);
}
}
And Registering and Unregistering method as below:
public void Registering() {
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
RegistrationId = GCMRegistrar.getRegistrationId(this);
if(RegistrationId.equals("")) {
GCMRegistrar.register(this, SENDER_ID);
}
}
public void Unregistering() {
Intent unregIntent = new Intent("com.google.android.c2dm.intent.UNREGISTER");
unregIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0));
startService(unregIntent);
}
On the same device, the first time call Registering()
, I got registered_id_1
.
And I call Unregistering()
, it will print:
Unregistered id: registered_id_1
means unregistering success.
And call Registering()
again, it will get another registered_id_2
.
And the push notification server I send message to registered_id_1
as below code:
Sender sender = new Sender("Android API Key");// Android API KEY
Message message = new Message.Builder().addData("message", My Message).build();
Result result = null;
try {
result = sender.send(message, registered_id_1, 5);
} catch (IOException e) {
e.printStackTrace();
}
But the client still receives the message that send to registered_id_1
.
What is wrong in my method?