Local Push service Uilocalnotification

Source: Internet
Author: User

Objective

Previous contact with the push service on iOS was done online push, although there are already many developers to provide integrated push service, but as a way to understand the principle is a good process.

Recently intends to apply the local push to send the function, would have thought it would be as troublesome as online push, but also to get the certificate, but also submit the verification, so has not been hands-on. After a little bit of understanding today, it's easy to find a local push.

Introduced

What is local push?

Online push is usually sent via the server to the corresponding device, and the corresponding device responds to notify the user. The local push is to notify the user directly locally by the program control to send the message. Because of the reduction of the server and the process of verifying the device, local push is actually a simple notification service.

What local push can do

GTD applications generally need to notify users of a task at a specified time based on their settings. The GTD transaction is basically backed up locally and, of course, locally to notify the user when the task is completed. Here, the role of the local push service is highlighted.

Some games, such as upgrading building equipment, usually take time to complete, and players cannot wait until the game. When the upgrade is over, the game pushes notifications locally to the player, the player responds and enters the game again, saving the player time and also bringing more traffic to the game's activation.

Although local push is not as high-frequency as online push, it is still useful.

How to implement local push

Add notification

The implementation of the local push is simple and more of a custom process. Take a look at the following code:

Uilocalnotification *notification = [[Uilocalnotification alloc] init];//ARC Environmentnotification.firedate= [NSDate Datewithtimeintervalsincenow:3];//Push TimeNotification.timezone=[Nstimezone defaulttimezone];notification.repeatinterval=0;//Push IntervalNotification.soundname= Uilocalnotificationdefaultsoundname;//Push SoundNotification.alertbody=@"Push Notification";//Push ContentNotification.applicationiconbadgenumber=1;//Number of messagesNotification.userinfo= [Nsdictionary dictionarywithobject:@"value"Forkey:@"Key"]; [[UIApplication sharedapplication] schedulelocalnotification:notification];

Instantiate a ' uilocalnotification ' object and set the properties of the object according to the specific requirements.

' Firedate ' indicates the time of the push. ' TimeZone ' is the corresponding time zone.

' Repeatinterval ' indicates the repetition interval of the push, whose type is ' nscalendarunit ', which can be assigned with this enumeration value, representing daily, weekly, or monthly. A 0 means that the push is not repeated.

' Soundname ' is a push-to-send sound that can be written on the name of a music file already in the bundle's resources, so you can customize the push sound.

' Alertbody ' is the content of the push.

' Applicationiconbadgenumber ' indicates the number of messages displayed on the app's icon. This property needs to be reset after receiving the message.

' UserInfo ' can store information about the notification.

Finally, the purpose of push notification is reached by calling ' Schedulelocalnotification '.

Receiving notifications

It is easier to receive push notifications, which can be received in the ' didreceivelocalnotification ' method of ' appdelegate '.

-(void) Application: (UIApplication *) application didreceivelocalnotification: (uilocalnotification*) Notification{nslog (@ "notification:%@"//  output push information  0  //  message count is 0}

The contents of the push message are printed out in the ' didreceivelocalnotification ' method, and the number of messages is set to 0. Of course, you can also determine the ' notification ' specific information to show the pop-up window, modify the number of messages.

Cancel Notification

// get local push array // Cancel a push // Cancel all pushes

The above code shows several APIs, but there is no necessary calling relationship.

' Scheduledlocalnotifications ' can get a local push array, where each member is uilocalnotification and can differentiate the type of push based on their userinfo.

' Cancellocalnotification ' can cancel a specific push notification. For example, cancel a notification obtained through ' scheduledlocalnotifications '.

' Cancelalllocalnotifications ' can cancel all notifications at once.

What are the holes in local push?

IOS8

After iOS8, you will need to register the notification type locally to use local push, or you will not receive a notification. The specific practice is to register in the ' didfinishlaunchingwithoptions ' of ' appdelegate '.

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *| Uiusernotificationtypebadge | Uiusernotificationtypesound Categories:nil]]; return YES;}

Music

Push notifications can customize the music, but not all music can be called. Currently only 30 seconds of music is supported, because the push can last up to 30 seconds.

Voice

If the phone is muted, the music will not play.

If you display a notification when your phone is turned on, it won't play music or even vibrate.

Applicationiconbadgenumber

If you click the notification from the notification bar to enter the app, it will respond to the notification callback, and the number of winning messages in the callback is set to 0.

And if the application is opened directly with the message, it does not go through the process of notifying the callback.

You can access this part of the logic as needed.

Summarize

The processing of local notifications is still relatively straightforward.

The implementation of the alarm clock is also based on local notifications, but it is more complex to consider such issues as vibration. There is a need to study slowly.

Local Push service Uilocalnotification

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.