iOS development-local notifications and remote notifications

Source: Internet
Author: User
Tags notification center

iOS development-local notifications and remote notificationsauthor Reichao concern 2016.02.01 00:18* Words 1921 Read 8898 comments 1 likes Here is the difference between push notifications and nsnotification:

1, Nsnotification is the system internal notification, generally used for internal event monitoring, or change of state and so on, is not visible
2, local notification and remote notification is visible, mainly used to inform users or send some app content updates, push some related messages, let users know what happened inside the app.


Paste_image.pngios Common Notifications

1. Local Push notification: (locally Notification)
2, the Remote Push notification: (remote Notification)

iOS Notification rendering effect (5)

1. Set the sound effect (reminder)
2. Set the banner,
3. Set the pop-up window,
4. Can also be displayed under the lock screen,
Number of 5.App icons (that is, the number of new content)
As for displaying banners or pop-up windows, depending on the user's settings
iOS Phone settings: Notification center-Select app-select notification mode under App-ok!


Note Points for paste_image.png notifications

1. When the app is running in the foreground, the notification will not be displayed.
2, click the notification, the default will automatically open the app for push notifications
3, regardless of whether the app is open, notifications can be issued as scheduled

1. Local PUSH notification:

Notifications you can send without needing to be networked
Usage scenarios:
Remind users to do some tasks, such as: timing reminders, life notes, watching movies, etc.

Push Notification Properties:
Enumeration value-time of notification (with limitations)@property (nonatomic) Nscalendarunit repeatinterval;Custom-time of notification (time can be set freely)@property (nonatomic,copy) Nscalendar *repeatcalendar;Area-Create only one center point and radius to create@property (nonatomic,copy) clregion *regionEnter the area to send a notification, set Yes, will only issue a notification, set no will be sent every time you enter this area@property (nonatomic,assign) BOOL regiontriggersonce NOSet the contents of a notification@property (nonatomic,copy) NSString *alertbody; //Decide whether alertaction is effective @property (nonatomic) BOOL hasaction;  Sets the text of the slider @property (nonatomic,copy) NSString *alertaction; //Set the start-up picture of the click Notification (General Settings app launch picture, here can write casually)@property (nonatomic,copy) nsstring *alertlaunchimage;  Set Alerttitle, which is the text above the notification content @property (nonatomic,copy) nsstring *alerttitle //Set the pop-up sound @property (nonatomic , copy) NSString *soundname; //Set the app's Message bar number @property (nonatomic) Nsinteger applicationiconbadgenumber; //settings Notification some additional data @property (nonatomic,copy) nsdictionary *userinfo;        
How to issue local notifications:
 1. Create a local notificationUilocalnotification *localnote = [[Uilocalnotification alloc] init];2. Set content for local notifications2.1. Set the time for the notification to be issued localnote.firedate = [NSDate Datewithtimeintervalsincenow:3.0];2.2. Set the content of the notification Localnote.alertbody =@ "What are you doing?";2.3. Set the text of the slider (lock screen State: swipe to "unlock") Localnote.alertaction =@ "unlock"; //2.4. Decide whether alertaction is effective localnote.hasaction = NO; //2.5. Set the start picture of the click notification localnote.alertlaunchimage = @ "123ABC"; //2.6. Setting Alerttitle Localnote.alerttitle = @" You have a new notice "; //2.7. Set the sound effects when notified Localnote.soundname = @" buyao.wav "; //2.8. Set the number in the upper-right corner of the application icon Localnote.applicationiconbadgenumber = //2.9. Set additional information Localnote.userinfo = @{@ "type": @1}; //3. Call notification [[uiapplication Sharedapplication] Schedulelocalnotification:localnote];}            

Use Note:
IOS7, no user authorization is required to issue a notification, and after iOS8, the user must authorize to send a notification

- (BOOL) Application: (UIApplication *) Application didfinishlaunchingwithoptions: (Nsdictionary *) Launchoptions {Set the application's icon in the upper-right corner of the number [application Setapplicationiconbadgenumber:0];if ([[Uidevice currentdevice].systemversion Doublevalue] >=8.0) {Uiusernotificationsettings *settings = [Uiusernotificationsettings Settingsfortypes:Uiusernotificationtypealert |Uiusernotificationtypebadge |Uiusernotificationtypesound Categories:NIL]; [Application registerusernotificationsettings:settings]; }Interface jumps (jumps in the state of the application being killed)In the kill state, the interface jump does not perform the following method-(void) Application: (UIApplication *) application didreceivelocalnotification: ( Uilocalnotification *) Notification,So when we write local notifications, we want to write in this and the following method, but to determine what type of notification to open theif (launchoptions[Uiapplicationlaunchoptionslocalnotificationkey]) {//Jump code UILabel *redview = [[UILabel alloc] init]; redview.frame = CGRectMake (0, 0, $  3 00); Redview.numberoflines = 0; redview.font = [uifont systemfontofsize:12.0]; redview.backgroundcolor = [  Uicolor Redcolor]; Redview.text = [nsstring stringWithFormat:@ "%@", launchoptions]; [Self.window.rootViewController.view Addsubview:redview];} return YES;}                

Monitoring notification, if the user opens the notification, can let the user into some specific interface

/* The application will execute this method at the front desk, or at the front desk */-(void) Application: (UIApplication *) Application didreceivelocalnotification: (Uilocalnotification *) notification{Must listen--the app jumps in the backgroundif (application.applicationstate = = uiapplicationstateinactive) { NSLog (@ "Jump on Interface"); //If you set some of the above notification methods, you can print the content of the additional information here, you can listen to the additional information, make the corresponding judgment NSLog (@ "%@", notification.userinfo);  UIView *redview = [[UIView alloc] init]; redview.frame = CGRectMake (0, 0,.  ); r Edview.backgroundcolor = [uicolor redcolor]; [Self.window.rootViewController.view Addsubview:redview];}}         
2. Remote PUSH notification:

Notifications from the remote server to the client (requires networking)
Remote Push Service---is APNs
Simulator cannot debug remote push

Why remote push notifications are required:

Address the limitations of getting traditional data and keep data up-to-date

Usage scenarios:

Chat function (usually non-instant chat), push the app's internal new features, version download, etc.

Note: All Apple devices, in the networked state, will be built with Apple's server-long connection

Effects of long connections:
Time validity, system upgrade, Find my iphone, remote notifications
Benefits:
Data transfer is fast and the status is kept up to date


Paste_image.png
Paste_image.png
Paste_image.png

I generally prefer to ask the interviewer the question during the interview. To explain clearly, there must be genuine development experience.

Using remote push

1. Get Apple to get devicetoken-unique identification (Bundel ID) for obtaining a mobile udid+ app
2. Get Apple back Devicetoken
3, send Devicetoken to the company's server
4, listen to the user click on the notification

How to debug remote push features

1. Real Machine
2. Debug the required certificate file for push
Certificate self-configuration ...
3. Launch app with push service
A CER certificate that contains a push + a computer with a publishing competency can publish

How to push
- (BOOL) Application: (UIApplication *) Application didfinishlaunchingwithoptions: (Nsdictionary *) Launchoptions {if ([[Uidevice currentdevice].systemversion Doublevalue] >=8.0) {IOS8Uiusernotificationsettings *settings = [Uiusernotificationsettings Settingsfortypes:Uiusernotificationtypealert |Uiusernotificationtypebadge |Uiusernotificationtypesound Categories:nil]; [Application registerusernotificationsettings:settings]; [Application registerforremotenotifications]; } else {//iOS7 [Application Registerforremotenotificationtypes:uiremotenotificationtypebadge | uiremotenotificationtypenewsstandcontentavailability | uiremotenotificationtypesound | uiremotenotificationtypealert]; }//Uiapplicationlaunchoptionsremotenotificationkey if (Launchoptions[uiapplicationlaunchoptionsremotenotificationkey]) { Span class= "hljs-comment" >//jump //add a red view} return yes;}              
//get Devicetoken -(void) application:(uiapplication *) application  Didregisterforremotenotificationswithdevicetoken:(NSData *) devicetoken{//pass Devicetoken to server  "%@", devicetoken.description);}         
// 正常接到远程通知的时候会调用这个方法- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{ NSLog(@"%@", userInfo); // 正常打开推送后,}
Background operations (depending on the push received, let the program do some of its own actions, regardless of whether the user clicks push)-(void) Application: (UIApplication *) Application didreceiveremotenotification: (Nsdictionary *) UserInfo Fetchcompletionhandler: (void (^) (uibackgroundfetchresult)) completionhandler{ NSLog (@ "---------"); UIView *redv = [[UIView alloc] init]; redview.backgroundcolor = [uicolor redcolor]; redview.frame = CGRect Make (   100); [Self.window.rootViewController.view Addsubview:redv]; //1. Turn on background mode 2. Tell the system if there is a new content Update 3. The notifications sent are in a fixed format ("content-available": "1") //2. Tell the system what's new Completionhandler (  Uibackgroundfetchresultnewdata);}               
1. Open Background mode
Paste_image.png2, tell the system what's new

completionHandler(UIBackgroundFetchResultNewData);

3. Send notification with fixed format

("content-available":"1")

Look for this picture: there are corresponding analog push code, need to contact me


Paste_image.png

In general development, direct use (jpush) laser push can quickly complete the push function of the app. In accordance with the official documentation. (Write a document specifically for laser push)

iOS Development

iOS development-local notifications and remote notifications

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.