2.iOS Apple Push-some sample code and attention issues

Source: Internet
Author: User
1. When the program starts every time you receive a push or the program does not start by pushing the message to start the program, will trigger the Appdelegate inside the method (note to the server with the certificate has been, such as the development environment, the server will also use the development certificate of the PEM to do):

-(void) Application: (UIApplication *) application didreceiveremotenotification: (nsdictionary *) userInfo

{

Here is the logical code after the push is received


Need to #import

Audioservicesplaysystemsound (1007); System's notification sound

Audioservicesplayalertsound (ksystemsoundid_vibrate);//Vibration


Custom Sound

NSString *path = [[NSBundle mainbundle] pathforresource:@ "message" oftype:@ "wav"];

Assemble and play sound effects

Systemsoundid Soundid;

Nsurl *filepath = [Nsurl fileurlwithpath:path isdirectory:no];

Audioservicescreatesystemsoundid ((__bridge cfurlref) FilePath, &soundid);

Audioservicesplaysystemsound (Soundid);

Sound stops

Audioservicesdisposesystemsoundid (Soundid);

}

The UserInfo default contains the following:

APS = {

alert = "";//push to display the problem information here

Badge = 0;//app Icon The push number in the upper right corner is set here

Sound = "";

};

If you need to add a custom field, let the server's small partners add an array to the same level as the APS (for example, in JSON):

{

"APS": {"alert": "This is the alert text", "badge": 1, "sound": "Default"},

"Server": {"ServerID": 1, "name": "Server Name")

}
-(BOOL) Application: (uiapplication*) Application didfinishlaunchingwithoptions: (nsdictionary*) launchOptions

Launchoptions will have userinfo information about the push message, at which point we can nsdictionary* remotenotification = [launchoptions objectforkey: Uiapplicationlaunchoptionsremotenotificationkey];

Gets the content of the push message. If the remotenotification is not empty, then the user enters through a push message, then a property can be declared @property (nonatomic) BOOL islaunchedbynotification;

Used to identify whether the user has entered the application by clicking on the notification message.
At this point,-(void) application: (uiapplication*) Application didreceiveremotenotification: (nsdictionary*) UserInfo

Must be invoked, IOS7 can be used-(void) application: (uiapplication*) Application didreceiveremotenotification: (nsdictionary*) UserInfo Fetchcompletionhandler: (void (^) (uibackgroundfetchresult)) Completionhandler

Because the Mainviewcontroller has been initialized since the invocation of this method, we have been able to register the listener for the push message in Mainviewcontroller to display the corresponding view, as follows://Subscribe to the Display view message, and will directly open a branch view [[ Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (presentview:) name:@ "Presentview" object: The nil];//pop-up message box prompts the user for a subscription notification message. Mainly for users when using the application, pop-up prompt box [[nsnotificationcenter defaultcenter] addobserver:self selector: @selector (shownotification:) name:@ "Notification" object:nil];

Therefore, in the Appdelegate didreceiveremotenotification can be judged by islaunchedbynotification to inform the different display methods.

use [[UIApplication sharedapplication] unregisterforremotenotifications];//logoff push message after exiting login

Ally explains: If the APP state is not running, this function will be invoked if the launchoptions contains uiapplicationlaunchoptionslocalnotificationkey to indicate the user clicks APN The notification causes the app to start running; if it does not contain the corresponding key value, the app is not started by clicking APN, and may be activated for direct click icon or otherwise.

-(BOOL) Application: (UIApplication *) Applicationdidfinishlaunchingwithoptions: (nsdictionary*) launchOptions; APN Content acquisition: nsdictionary *remotenotification = [Launchoptionsobjectforkey:
Uiapplicationlaunchoptionsremotenotificationkey]

If the app is running in the foreground or in the background, this function will be invoked, and it can be uiapplicationstateactive to determine whether the program is running in the foreground via Appdelegate applicationstate. This situation is handled in this function:

-(void) application: (uiapplication*) Application didreceiveremotenotification: (nsdictionary*) UserInfo; APN content is UserInfo

If you are using the Remote Notification feature of IOS 7, then the processing function needs to be used--this feature is shown in Blog 4

-(void) application: (uiapplication*) Application didreceiveremotenotification:
(Nsdictionary *) Userinfofetchcompletionhandler: (void (^) (uibackgroundfetchresult)) Completionhandler; APN content is UserInfo

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.