使用Codenameone显示推送通知

时间:2015-12-28 21:42:35

标签: java notifications push-notification codenameone

我是Codename One的初学者;我不知道如何发送推送通知并显示主题。我正在使用此代码:

public class PushDemo implements PushCallback {

    private Form current;
    public void init(Object context) {

     }
     public void start() {
         if(current != null){
             current.show();
             return;
         }
        new StateMachine("/theme");
     }

     public void stop() {
        current = Display.getInstance().getCurrent();
     }

     public void destroy() {

     }

     public void push(String value) {
        Dialog.show("Push Received", value, "OK", null);
     }

     public void registeredForPush(String deviceId) {
        Dialog.show("Push Registered", "Device ID: " + deviceId + "\nDevice Key: " +Push.getDeviceKey() , "OK", null);
     }

     public void pushRegistrationError(String error, int errorCode) {
        Dialog.show("Registration Error", "Error " + errorCode + "\n" + error, "OK", null);
     }

}

1 个答案:

答案 0 :(得分:0)

这里有一个推动的游戏: https://www.codenameone.com/blog/building-a-chat-app-with-codename-one-part-6.html

首先,您需要设置以下常量来实际注册/发送推送:

private static final String PUSH_TOKEN = "********-****-****-****-*************";
private static final String GCM_SENDER_ID = "99999999999999";
private static final String GCM_SERVER_API_KEY = "******************-********************";
private static final boolean ITUNES_PRODUCTION_PUSH = false;
private static final String ITUNES_PRODUCTION_PUSH_CERT = "https://domain.com/linkToP12Prod.p12";
private static final String ITUNES_PRODUCTION_PUSH_CERT_PASSWORD = "ProdPassword";
private static final String ITUNES_DEVELOPMENT_PUSH_CERT = "https://domain.com/linkToP12Dev.p12";
private static final String ITUNES_DEVELOPMENT_PUSH_CERT_PASSWORD = "DevPassword";

您缺少应该在start()方法结束时的注册调用:

Display.getInstance().callSerially(() -> {
    // registering for push after the UI appears
    Hashtable args = new Hashtable();
    args.put(com.codename1.push.Push.GOOGLE_PUSH_KEY, GCM_SENDER_ID);
    Display.getInstance().registerPush(args, true);
});

然后您可以使用以下内容向设备发送推送:

Push.sendPushMessage(PUSH_TOKEN, textToShowToTheUser + ";" + hiddenPayload,
                ITUNES_PRODUCTION_PUSH, GCM_SERVER_API_KEY, cert,pass, 3, pid);