iOS does not use third-party platforms to send push messages

Source: Internet
Author: User

iOS does not use third-party platforms to send push messages

First look at the client:

There are two points to look at: The first part of the codeDevicetoken get, and look at the code

-(BOOL) Application: (uiapplication *) application didfinishlaunchingwithoptions: ( Nsdictionary *) launchoptions

{

//type of message push support

uiremotenotificationtype types =

(Uiremotenotificationtypebadge

     | Uiremotenotificationtypesound

     | Uiremotenotificationtypealert);

//Register message push

[[uiapplication sharedapplication] registerforremotenotificationtypes: Types];

//Override point for customization after application launch.

return YES;

}

Get Devicetoken Success

-(void) Application: (uiapplication *) application

Didregisterforremotenotificationswithdevicetoken: (nsdata *) Devicetoken

{

nsstring *pushtoken = [[[[Devicetoken description]

stringbyreplacingoccurrencesofstring:@ "<" withstring : @ "" ]

@ ">" &NBSP; withstring: Span style= "Color:rgb (209,47,27);" >@ ""

stringbyreplacingoccurrencesofstring:@ "" withstring: c4>@ ""];

NSLog (@ "devicetoken:%@", Pushtoken);

//The operation here is to send device token to the server

}

Note: Here is a small trick, how to nsdata data content inside the "<", ">", "" to remove, to get a valid Devicetoken.

Registration Message Push failed

-(void) Application: (uiapplication *) application

Didfailtoregisterforremotenotificationswitherror: (nserror *) error

{

NSLog(@ "Register Remote notifications error:{%@}", [Error Localizeddescription]);

}

Handling received message pushes

-(void) Application: (uiapplication *) application

Didreceiveremotenotification: (nsdictionary *) userInfo

{

NSLog(@ "Receive Remote notification:%@", userInfo);

nsdictionary *aps = [UserInfo valueforkey:@ "APS"];

nsstring *content = [APs Valueforkey:@ "alert"]; // push the displayed content

Uialertview *alert =

[[uialertview alloc] initwithtitle:@ " warm tips "

Message: Content

delegate:Nil

cancelbuttontitle:@ " OK "

otherbuttontitles:nil];

[Alert show];

}


Second, make a certificate with push message

Go to the Apple development website:

650) this.width=650; "class=" Confluence-embedded-image "src="/download/attachments/5505262/ Fa4e14b8-81b6-4e73-bd44-e09973b0957a.png?version=1&modificationdate=1409809851313&api=v2 "style=" margin : 0px;padding:0px;border:0px; "alt=" Fa4e14b8-81b6-4e73-bd44-e09973b0957a.png "/>

Check with push service:

650) this.width=650; "class=" Confluence-embedded-image "src="/download/attachments/5505262/ E2b204df-1c00-44a5-afdc-b4d3bb364e3b.png?version=1&modificationdate=1409809937478&api=v2 "style=" margin : 0px;padding:0px;border:0px; "alt=" E2b204df-1c00-44a5-afdc-b4d3bb364e3b.png "/>

After the successful creation, download the certificate double click and you can see it in the keychain:

650) this.width=650; "class=" Confluence-embedded-image "src="/download/attachments/5505262/ 79eef468-92e3-46f8-9681-87f564c4354a.jpeg?version=1&modificationdate=1409810340413&api=v2 "Style=" margin:0px;padding:0px;border:0px; "alt=" 79eef468-92e3-46f8-9681-87f564c4354a.jpe "/>

Right-click Export P12 file, you can set the password, or can not set, generally not. The above certificate is OK.

Here's a look at Java-written server code:

Package Com.sdunicom.iphone.apns;

Import Javapns.back.PushNotificationManager;
Import Javapns.back.SSLConnectionHelper;
Import Javapns.data.Device;
Import Javapns.data.PayLoad;

public class Mainsend {
public static void Main (string[] args) throws Exception {
try {
String Devicetoken = "56378f94d620b0210a9228ea513a4ba2cbe61d0b29143116812da411009c0c9e";

payload PayLoad = new PayLoad ();
Payload.addalert ("Shengkovi's fellow citizens, hello everyone");
Payload.addbadge (1);//The number of message push marks, the number shown in the small red circle.
Payload.addsound ("Default");

Pushnotificationmanager Pushmanager = Pushnotificationmanager.getinstance ();
Pushmanager.adddevice ("IPhone", Devicetoken);

//connect to APNs
String host= "gateway.sandbox.push.apple.com";
int port = 2195;
String certificatepath= "/users/wangjinhan/desktop/Recent technical research/java background push program/DEVELOPCM.P12";
String certificatepassword= "";
Pushmanager.initializeconnection (Host,port, Certificatepath,certificatepassword, SSLCONNECTIONHELPER.KEYSTORE_TYPE_PKCS12);

//send Push
Device client = Pushmanager.getdevice ("IPhone");
Pushmanager.sendnotification (client, payLoad);
Pushmanager.stopconnection ();

Pushmanager.removedevice ("IPhone");
}
catch (Exception e) {
E.printstacktrace ();
}

}
}

/***********************

The code has several points to note:

1.String Devicetoken = "56378f94d620b0210a9228ea513a4ba2cbe61d0b29143116812da411009c0c9e";

To be sent to the corresponding device

2.payload.addbadge (1);

The number of message push marks, the number shown in the small red circle. On the server to make a cumulative, when the click on the count in order to, if no view has been accumulated.

3.String certificatepath= "/users/wangjinhan/desktop/Recent technical research/java background push program/DEVELOPCM.P12";

The path to the certificate cannot be faulted

4.String certificatepassword= "";

Export the password for the certificate settings, without setting the password, as above

So you can push it.

***********************/


This article is from the "8830148" blog, please be sure to keep this source http://8840148.blog.51cto.com/8830148/1548731

iOS does not use third-party platforms to send push messages

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.