简体   繁体   English

使用Codenameone显示推送通知

[英]display push notifications with Codenameone

I'm a beginner with Codename One; 我是Codename One的初学者; I don't know how can I send push notifications and display a theme. 我不知道如何发送推送通知和显示主题。 I'm using this code: 我正在使用此代码:

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

}

There is a walkthru of push here: https://www.codenameone.com/blog/building-a-chat-app-with-codename-one-part-6.html 此处有很多建议: https : //www.codenameone.com/blog/building-a-chat-app-with-codename-one-part-6.html

First you need to set the following constants to actually register/send the push: 首先,您需要设置以下常量以实际注册/发送推送:

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";

You are missing the register call which should go in the end of your start() method: 您缺少应该在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);
});

Then you can send a push to a device using something like this: 然后,您可以使用以下方式将推送发送到设备:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM