IOS remote notification (Notification) and local notifications (locally Notification)

Source: Internet
Author: User

iOS notifications are divided into remote notifications and local notifications, remote notifications need to connect to the network, local notifications are not required, regardless of whether the user opens the app or closes the app, our notifications will be sent and received by the client

We use remote notification is mainly to update the latest data to the user at any time, using local notification is mainly to remind users to complete some tasks

  

  Remotely notify remote Notification:

Its main working principle is: The client sends its own UUID and bundle ID to Apple's APNS server--and Apple's APNs server is encrypted and returns a Devicetoken to the client-- When the client gets Devidetoken, it sends it to the server provided by the app, and the server stores the client's devicetoken to the database, and when the server sends the remote notification to the client, Will get this client's devicetoken--> in the database to send data to Apple's APNs server before sending it to the client

Remote notification requires a real machine , and you also need to go to the Apple Developer Center to request a certificate: A Real Machine Debug certificate, a remote push certificate (which computer to debug or publish which app), a description of the file certificate (which computer uses which device to debug which app)

We can use Pushmebaby to simulate a server, or we can use third-party software to send notifications such as Jpush, etc.

  The following is the implementation of the Code:

1-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) Launchoptions2 {3     if([Uidevice Currentdevice].systemversion.doublevalue <8.0){//less than iOS84     5Uiremotenotificationtype type = Uiremotenotificationtypealert | Uiremotenotificationtypesound |Uiremotenotificationtypebadge;6         7         //system automatically sends UUID and bundle ID to Apple APNs server8 [Application Registerforremotenotificationtypes:type];9}Else{//greater than or equal to iOS8Ten      OneUiusernotificationtype type = Uiusernotificationtypebadge | Uiusernotificationtypealert |Uiusernotificationtypesound; A          -Uiusernotificationsettings *settings =[uiusernotificationsettings settingsfortypes:type categories:nil]; -         //Notification Type the [Application registerusernotificationsettings:settings]; -          -         //Registration Notice - [Application registerforremotenotifications]; +     } -      +     //can get to userinfo data ANsdictionary *userinfo =Launchoptions[uiapplicationlaunchoptionsannotationkey]; at      -     returnYES; - } -  - //Get Devicetoken -- (void) Application: (UIApplication *) application Didregisterforremotenotificationswithdevicetoken: (NSData *) Devicetoken in { -NSLog (@"%@", Devicetoken); to } +  - //iOS7 is called before, the content that receives the remote notification is called the //The program is open, regardless of the foreground or background, this method is called * //if the program is off state does not call this, the application:didfinishlaunchingwithoptions will be called: $- (void) Application: (UIApplication *) application didreceiveremotenotification: (Nsdictionary *) UserInfoPanax Notoginseng { -NSLog (@"%@", userInfo); the } +  A //called after iOS7, if the contents of a remote notification are received, this method is called the- (void) Application: (UIApplication *) application didreceiveremotenotification: (nsdictionary *) userInfo Fetchcompletionhandler: (void(^) (Uibackgroundfetchresult)) Completionhandler + { -     //This method calls this block to notify the system to update the UI interface $     //Uibackgroundfetchresultnewdata, receiving the data $     //Uibackgroundfetchresultnodata, no data received -     //uibackgroundfetchresultfailed receiving data failed - Completionhandler (uibackgroundfetchresultnewdata); the      -}

  

  Local notification locally Notification

  Basic Properties and methods:

Property:

    • Specify when notifications are sent: NSDate *firedate
    • Specify the time zone for sending notifications: Nstimezone *timezone
    • Repeat cycle: Repeatinterval
    • Notice content: NSString *alertbody
    • Title of the lock screen status: NSString *alertaction
    • Click the start image after notification: NSString *alertlaunchimage
    • Receive notification music played: NSString *soundname
    • Icon Reminder Number: Nsinteger Applicationiconbadgenumber
    • Additional Information: Nsdictionary *userinfo

  Method:

    • Immediate execution:-(void) Presentlocalnotificationnow: (uilocalnotification *) notification
    • Registration notification, executed according to the specified send time:-(void) Schedulelocalnotification: (uilocalnotification *) notification
    • To cancel a single notification:-(void) Cancellocalnotification: (uilocalnotification *) notification
    • Cancel all notifications:-(void) cancelalllocalnotifications

  Here is the code implementation:

1 //To create a local notification object2Uilocalnotification *noti =[[Uilocalnotification alloc] init];3     4     //Specify when notifications are sent 10s5Noti.firedate = [NSDate datewithtimeintervalsincenow:10.0f];6     //Specify time zone7Noti.timezone =[Nstimezone Defaulttimezone];8     //Specify notification content9Noti.alertbody =@"This is the content of the notification";Ten      One     //set Notification recurrence period (1 minutes) ANoti.repeatinterval =Nscalendarunitsecond; -      -     //Specifying the lock screen information theNoti.alertaction =@"This is information on the lock screen interface."; -      -     //set the start picture when the click Notification enters the program -Noti.alertlaunchimage =@"XXX"; +      -     //receive notifications to play music +Noti.soundname =@"Hehe.wav"; A      at     //set the application's reminder icon -Noti.applicationiconbadgenumber = About; -      -     //You can specify the data to be passed after you click Notifications in the future when registering for notifications -Noti.userinfo = @{@"Dogname":@"xx1", -                       @"Weight":@( -) in                       }; -      to     //Sign up to add notifications +UIApplication *app =[UIApplication sharedapplication]; -[App Schedulelocalnotification:noti];

  Note: Pre-registration notification types are required in iOS8

1-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions {2     //Override point for customization after application launch.3     4     //Note: In iOS8, the notification type must be registered in advance5     if([Uidevice Currentdevice].systemversion.doublevalue >=8.0) {6Uiusernotificationtype type = Uiusernotificationtypealert | Uiusernotificationtypebadge |Uiusernotificationtypesound;7Uiusernotificationsettings *settings =[uiusernotificationsettings settingsfortypes:type categories:nil];8         //Registration Notification Type9 [Application registerusernotificationsettings:settings];Ten     } One } A  - //received local notification will be called, the foreground automatic call, background click on the notification after the call -- (void) Application: (UIApplication *) application didreceivelocalnotification: (Uilocalnotification *) Notification the { -NSLog (@"%@", notification.userinfo); -}

  

IOS remote notification (Notification) and local notifications (locally Notification)

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.