IOS Local Notifications

Source: Internet
Author: User

If the user has not used our app for a long time, we need to remind the user to use it. This local notification can be done.

First explain my solution: write in Appdelegate

1, when the user exits the app to create a notification, a certain time after the call, such as 10 seconds.

//How to enter the background response-(void) Applicationdidenterbackground: (UIApplication *) application{//Initialize local notification object Uilocalnotification *notification =[[Uilocalnotification alloc] init];If(notification) {//Set reminder time for notifications nsdate *currentdate =[NSDate Date]; Notification.timezone = [Nstimezone defaulttimezone];//Use local time zone notification.firedate = [currentdate datebyaddingtimeinterval:10];//Set the text content of a reminder Notification.alertbody =@"You haven't been paying attention to your baby for a week, come and see!"; Notification.alertaction = nslocalizedstring (@"Caring for your baby", nil);//Notification tones use the default notification.soundname=Uilocalnotificationdefaultsoundname; // Set the number of reminders in the top right corner of the application notification.applicationiconbadgenumber++; // Set the userinfo of the notification to identify the notification nsdictionary *dict =[nsdictionary dictionarywithobjectsandkeys:@ "  Notification",@"nfkey", nil]; [Notification setuserinfo:dict]; // Add notifications to the system [[UIApplication sharedapplication] schedulelocalnotification:notification];}}  

2, in the receipt of notification, click into the application when the cancellation notice, the outside display of the number assigned to 0,application.applicationiconbadgenumber=0;

Didreceivelocalnotification is the app that runs in the foreground, notifies the time to call the method. If the program is running in the background, the time will not go this way.

Applicationdidbecomeactive is the app running in the background, the notification time, you enter from the notification bar, or directly click the app icon to enter, the way to go.

-(void) applicationdidbecomeactive: (uiapplication *) application {            application.applicationiconbadgenumber=0;     }   
-(void) Application: (UIApplication *) Application Didreceiveremotenotification :(nsdictionary *) userinfo{Application.applicationiconbadgenumber=0for (uilocalnotification *noti in [application scheduledlocalnotifications]) {nsstring *notiid = [noti.userinfo objectforkey:

3, when the user does not receive notification to enter the application of the time to cancel the notification. Cause: When you exit the program for the first time, you will create a notification, a, 10 seconds after the push, if in this 10 seconds, re-login to exit and create a new notification B, then we will receive two notifications in a row. To avoid duplication, sign in to the app when notice a is not yet in case we cancel notification A and create notification B upon exiting.

-(void) Applicationwillenterforeground: (uiapplication *) application{  // Cancel all notifications     [ Application cancelalllocalnotifications];    }    

If you want to call this notification repeatedly,

// 设置重复间隔

 notification.repeatInterval = kCFCalendarUnitDay;

IOS Local 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.