GCM实现在Android上的推送通知 [英] GCM to implement push notification on Android

查看:179
本文介绍了GCM实现在Android上的推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是客户端的我的code:

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);
}

在相同的设备,第一时间呼叫注册(),我得到了 registered_id_1 。结果
我呼吁注销(),它将打印:
   未注册ID:registered_id_1 表示注销的成功结果。
并调用注册()再次,它会得到另一个 registered_id_2

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.

和推送通知服务器我将消息发送到 registered_id_1 如下code:

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();
}

但客户仍收到 registered_id_1 的发送到邮件中。结果
什么是错误的,我的方法?

But the client still receives the message that send to registered_id_1.
What is wrong in my method?

推荐答案

有什么错在你的方法。这是GCM的行为。当设备得到一个新的注册ID为给定的应用程序,旧的注册ID previously分配给该设备和应用程序仍然可以工作。如果您在使用旧的注册ID发送消息GCM,您将获得包含规范注册ID(其值是该设备的新注册ID)的响应。当你得到这样的回应,你应该消除您的服务器的数据库旧的注册ID。

There is nothing wrong in your method. That's the behavior of GCM. When a device gets a new registration ID for a given app, the old registration IDs previously assigned for this device and app will still work. If you send a GCM message using the old registration ID, you'll get a response containing a canonical registration ID (whose value is the new registration ID for that device). When you get such a response, you should eliminate the old registration ID from your server's DB.

或者,你可以处理它更好,如果在取消注册旧注册ID你还可以通知你的服务器,这将删除旧的注册ID,从不再次尝试发送邮件到它。

Or you can handle it better if when un-registering the old registration ID you'll also notify your server, which will remove the old registration ID and never try to send a message to it again.

这篇关于GCM实现在Android上的推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆