First, Introduction
Divided into local push and remote push 2 kinds. You can give the user a hint when the app is not open or even the phone lock screen. They all need to register, after registering the system will pop up a prompt box (such as) prompt the user whether consent, if the consent is normal use; If the user does not agree to the next time the program will not pop up the prompt box, you need to set the user to the settings. A total of three types of tips:
- Uiusernotificationtypebadge: Information tip in the upper right corner of the app icon
- Uiusernotificationtypesound: Play beep
- Uiusernotificationtypealert: Prompt box
Second, local push
1 Registration and processing
The code is as follows:
Generally at the start of the registration notice, the program is killed, click on the notification after the call this program-(BOOL) Application: (UIApplication *) Application didfinishlaunchingwithoptions: (Nsdictionary *) Launchoptions {if ([[Uidevice currentdevice].systemversion Floatvalue] >=8.0) {IOS8Uiusernotificationsettings *setting = [uiusernotificationsettings settingsfortypes: Uiusernotificationtypebadge | uiusernotificationtypealert | uiusernotificationtypesound categories:nil]; [Application registerusernotificationsettings:setting]; } if (Launchoptions[ Uiapplicationlaunchoptionslocalnotificationkey]) {//Add processing code here} return yes;} ///program is not killed (in the foreground or in the background), click on the notification will call this program-(void) Application: (uiapplication *) application didreceivelocalnotification: ( Uilocalnotification *) Notification {//Add processing code here}
As you can see, there are two ways to process code, and one is
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;
The other is
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
If the program is not killed, either in the foreground or in the background, the former is called, and if the program is killed, the latter is called.
2 Sending Notifications
The code is as follows
- (Ibaction) Addlocalnotification {1. Create a local notificationUilocalnotification *localnote = [[Uilocalnotification alloc] init];1.1. Set the time for the notification to be issued localnote.firedate = [NSDate Datewithtimeintervalsincenow:5];1.2. Set the notification content Localnote.alertbody =@ "This is a push it is a push";1.3. When setting the lock screen, a text displayed below the font localnote.alertaction =@ "hurriedly!!!!!"; Localnote.hasaction = YES; //1.4. Set the boot picture (opened by notification) Localnote.alertlaunchimage = @ "... /documents/img_0024.jpg "; //1.5. Set the sound through arrival localnote.soundname = uilocalnotificationdefaultsoundname; //1.6. Set the app icon to display the upper left corner of the number localnote.applicationiconbadgenumber = 999; //1.7. Set some additional information localnote.userinfo = @{@ "QQ": @ "704711253", @ " msg": @ "Success"}; //2. Execution notice [[uiapplication sharedapplication] schedulelocalnotification:localnote];}
The effect is as follows:
3 Cancellation Notice
// 取消所有本地通知[application cancelAllLocalNotifications];
Third, remote push
Unlike our own push service on Android, Apple controls the device very strictly, and the process of message push must go through APNs (Apple push Notification service).
In general, if a program back to the background can not run the code (Audio, VOIP, etc. can be run in the background), or the program exits, then it and the corresponding application of the background server disconnected, will not receive the information sent by the server, But each device as long as the network will be and Apple's APNs server to establish a long connection (persistent IP connection), so long as the APNs server through Apple, our own server can be indirectly and the device remains connected, as follows:
Steps to use:
1 Check backgroud Modes, remote notifications, mainly after the iOS7, Apple supports the background to run, if this opened, when the remote push received, the program can also do some processing in the background, as shown in:
2 Remote push registration is different from local push, iOS8.0 is different before and after, code see below.
In addition, the first time you use push, there may be a question: it didFinishLaunchingWithOptions will be called every time the program is opened, that is not every time the registration function is called, each time the window asks the user "whether to allow push notifications"? In fact, this window will only pop up the first time you open the program, regardless of whether the user allows or does not allow Apple to remember the user's choice, the registration function calls many times to the user also has no effect
- (BOOL) Application: (UIApplication *) Application didfinishlaunchingwithoptions: (Nsdictionary *) Launchoptions {IOS8 should be treated differently after and beforeif ([[Uidevice currentdevice].systemversion Floatvalue] >=8.0) {Uiusernotificationsettings *settings = [Uiusernotificationsettings Settingsfortypes:Uiusernotificationtypebadge |Uiusernotificationtypealert |uiusernotificationtypesound categories:nil]; [[uiapplication sharedapplication] registerusernotificationsettings:settings];} else {[Application Registerforremotenotificationtypes: Uiremotenotificationtypealert | uiremotenotificationtypebadge | uiusernotificationtypesound]; } return yes;} ///the meaning of this function is that when the user closes the notification in the settings, this function is called when the program starts and we can get the user's settings-(uiapplication *) application didregisterusernotificationsettings: ( Span class= "hljs-built_in" >uiusernotificationsettings *) notificationsettings {[Application Registerforremotenotifications];}
3 If registration fails, such as no certificate, etc., it will be called:
/// 注册失败调用- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { NSLog(@"远程通知注册失败:%@",error);}
4 Get Devicetoken
If the user agrees, Apple will generate Devicetoken based on the app's Bundleid and phone udid, then call application's Didregister method to return to Devicetoken, and the program should send the Devicetoken to the app's service Server is obligated to store it (multiple devicetoken may be stored if multiple-point logons are allowed). Devicetoken will also change: "If the user restores backup data to a new device or computer, or reinstalls the operating system, the D Evice token changes ", so should be sent to the server every time (provider)
/// 用户同意后,会调用此程序,获取系统的deviceToken,应把deviceToken传给服务器保存,此函数会在程序每次启动时调用(前提是用户允许通知)- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSLog(@"deviceToken = %@",deviceToken);}
5 Users clicked the notification
The program is opened by default. There are three functions for handling code, iOS7 before and after the program is in the background
- 5.1 IOS7 and the aftermath
This function whether the program is killed or in the background, as long as the user clicked on the notification, will be called, so if it is iOS7, then do not have to do in didfinishlaunchingwithoptions, only in the following function to do processing, You should avoid repeating processing in the Didfinishlaunchingwithoptions function at this time.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { // userInfo}
Note: When running in the background in the first step, the user does not click on the notification and can also perform:
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo fetchCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler
When the user clicks on the notification, if the program is killed it will call the following first function, if the program is in the background call the following second function, so the following two functions should be used with a
- (bool) Application: (uiapplication *) application Didfinishlaunchingwithoptions: (nsdictionary *) launchoptions { //get remote Push message nsdictionary *userinfo = Launchoptions[uiapplicationlaunchoptionsremotenotificationkey]; if (userInfo) {//have push messages, processing push messages} return yes;} ///iOS3 only after the program is in the background, the user clicked the notification will not be called, should be used with Didfinishlaunchingwithoptions-(void) Application: (uiapplication *) application Didreceiveremotenotification: (nsdictionary *) userInfo {// UserInfo}
In the actual programming, if you want to be compatible with IOS7 before, three functions can be used at the same time, are listed, the system will automatically select the appropriate call.
6 summarize the invocation of the function:
Start after first installation:
- Didregisterforremotenotificationswithdevicetoken is called
- The system asks the user if they agree to receive notifications
- Didregisterusernotificationsettings is called regardless of the user's choice of consent or rejection
When you apply a non-first boot:
7 service-Side push formats
{ "aps" : { // 必须有 "alert" : "string", "body" : "string", "badge" : number, "sound" : "string" }, "NotiId" : 20150821, // 自定义key值}
8 Push size limits
The size of the remote notification payload varies depending on the API used by the server. When using the HTTP/2 provider API, the load is up to 4kB, and when legacy binary interface is used, the load is maximum 2kB. APNs will refuse to send this notification when the payload size exceeds the specified load size.
9 overall as shown (take the push as an example):
10 Finally, you will also need to apply for a certificate, no longer details-_-
iOS local push and remote push detailed