Write before:
1.unipush supports many push templates. Friends who are interested in learning about it can Official website View: if you want to push ios, you can only use the passthrough template. If you want to push Android and ios, you need to make some changes in the passthrough template code of the official website example;
2. Even though the code and push channel are so robust, I believe that the success rate of push is still impossible to be 100%. This is a point that android platform needs to pay special attention to. unipush provides a scheme of SMS completion. Friends in need can learn about it: Portal.
3. Due to different requirements, the code of this post is not necessarily applicable to all businesses, so this post is only for introduction. Secondly, the author's time is limited. The following code is only for the implementation of a single client push. If there are other problems, please leave a message for communication. If there are problems in the content, please comment and point out.
The code can achieve the following effects:
1. No matter ios or android app, when the app is advanced, it does not push the notification in the system notification bar, but it can push the messages that are not displayed in the system notification bar to the open app;
2. Once the app is in the exit state or in the destroyed state, the notification push will be performed in the system notification column.
Note:
1. After exiting the app or destroying the app process, the push scheme is implemented by the manufacturer push scheme of unipush, so if you want to implement offline push, you must ensure that the manufacturer push configuration of unipush platform is correct, which is generally free of problems according to the documents on the official website, so we will not repeat here.
1. Get parameters such as AppID, AppKey, MasterSecret, etc., which should not be described too much,
2. For the integration of unipush sdk and pom.xml, add the following configuration. For the integration mode of non maven projects or more details, please stamp: http://docs.getui.com/getui/server/java/guide/
<dependencies> <dependency> <groupId>com.gexin.platform</groupId> <artifactId>gexin-rp-sdk-http</artifactId> <version>4.1.0.5</version> </dependency> </dependencies> <repositories> <repository> <id>getui-nexus</id> <url>http://mvn.gt.igexin.com/nexus/content/repositories/releases/</url> </repository> </repositories>
3. Write the getAPNPayload method (Note: this method is implemented to push ios, which will be ignored by Android client). For more methods, please stamp: http://docs.getui.com/getui/server/java/template/?id=doc-title-7
private static APNPayload getAPNPayload() { APNPayload payload = new APNPayload(); //Add 1 display on the basis of existing numbers. When it is set to - 1, subtract 1 display from existing numbers. When it is set to number, display the specified number payload.setAutoBadge("+1"); payload.setContentAvailable(1);//Push directly with transparent data //The Dictionary type sound can be used for ios 12.0 and above payload.setSound("default"); payload.setCategory("$Defined by client");//Trigger specific action and button display in the client notification bar payload.addCustomMsg("Custom message by customer key", "Custom message by customer value");//Add customized data in the form of key value //Simple mode APNPayload.SimpleMsg payload.setAlertMsg(new APNPayload.SimpleAlertMsg("hello"));//Notification message body SimpleAlertMsg: notification text message string // payload.setAlertMsg(getDictionaryAlertMsg()); / / dictionary mode uses APNPayload.DictionaryAlertMsg //Set voice broadcast type, int type, 0. Unavailable 1. Play body 2. Play custom text // payload.setVoicePlayType(2); //Set voice broadcast content, String type, non required parameter, user-defined broadcast content, only effective when voicePlayMessage=2 //Note: when "definition type" = 2, "definition content" is empty, ignore not playing // payload.setVoicePlayMessage("define content"); / / set the voice broadcast content, String type, non required parameter, user-defined play content, only effective when voicePlayMessage=2 // Add a multimedia resource. Currently, at most 3 resources are passed in // payload.addMultiMedia(new MultiMedia().setResType(MultiMedia.MediaType.pic) // . setResUrl("resource file address") // .setOnlyWifi(true)); return payload; }
3. Write transparent template code:
public static TransmissionTemplate getTransmissionTemplate() { TransmissionTemplate template = new TransmissionTemplate(); template.setAppId(APPID); template.setAppkey(APPKEY); template.setTransmissionType(1);//Used with transmissionContent, the optional values are 1 and 2; 1: start APP immediately (not recommended, affecting customer experience); 2: the client needs to handle the message after receiving it template.setTransmissionContent("red"); //Transparent content, escape character not supported template.setAPNInfo(getAPNPayload()); //ios message push is used to set the title, content, voice, multimedia, VoIP (voice transmission based on IP), etc. Effect when APNs are taken offline Notify notify = new Notify(); notify.setTitle("Vendor push title"); notify.setContent("Vendor push content"); notify.setIntent("intent:#Intent;action=android.intent.action.oppopush;launchFlags=0x14000000;component=com.wosiwz.xunsi/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title=title;S.content=content;S.payload=test1;end"); notify.setType(GtReq.NotifyInfo.Type._intent); template.set3rdNotifyInfo(notify);//Set up third party notifications return template; }
4. Write getSingleMessage class. There are some problems with the code on GitHub. Readers can pay attention to the comments. I have written the problems in the comments:
private static SingleMessage getSingleMessage(AbstractTemplate template) { SingleMessage message = new SingleMessage(); message.setData(template); // Set message offline and offline time message.setOffline(true); // Offline effective time, in milliseconds, optional message.setOfflineExpireTime(72 * 3600 * 1000); message.setPriority(1); /** * !!!2020 On January 16, 2016, at 16:34:16 (Thursday), there is a hole in the code on GitHub. If the transparent message setPushNetWorkType is 1, the client can't push with wifi!!! * Official website notes: * Push network requirements * 0:Unlimited networking mode; * 1:Only wifi; * 2:Mobile network only * */ message.setPushNetWorkType(0); // Determine whether the client pushes in wifi environment. 1 is push only in wifi environment, 0 is unlimited network environment, default unlimited return message; }
5. Write push main method test:
private static void pushToSingle() { AbstractTemplate template = PushTemplate.getTransmissionTemplate(); //Transparent message template SingleMessage message = getSingleMessage(template); Target target = new Target(); target.setAppId(APPID); target.setClientId(CID); IPushResult ret = null; try { ret = push.pushMessageToSingle(message, target); } catch (RequestException e) { e.printStackTrace(); ret = push.pushMessageToSingle(message, target, e.getRequestId()); } if (ret != null) { System.out.println(ret.getResponse().toString()); } else { System.out.println("Server response exception"); } }
6. So far, no matter whether the app is open or the process is destroyed, push should be able to be pushed to the mobile phone.
More information:
unipush GitHub homepage (at 16:34:16 on January 16, 2020, I see that there is a problem with the code on GitHub, which I have pointed out in step 4. You can pay attention to it): https://github.com/GetuiLaboratory/getui-pushapi-java-demo
unipush management console: https://dev.dcloud.net.cn/
Iron root Published 87 original articles, won praise 22, visited 190000+ Private letter follow