iOS development-Local push notifications

Source: Internet
Author: User

Not much to say directly on the code

#import "AppDelegate.h"

@interface Appdelegate ()

@end

@implementation Appdelegate

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {

Override point for customization after application launch.

If you have received authorization to send notifications, create a local notification, or request authorization (note: If you do not request authorization in the settings there is no corresponding notification settings item, that is, if the request has never been sent, even through the settings can not open the message allowed setting)

if ([[UIApplication sharedapplication]currentusernotificationsettings].types!=uiusernotificationtypenone) {

[Self addlocalnotification];

}else{

[[UIApplication Sharedapplication]registerusernotificationsettings:[uiusernotificationsettings Settingsfortypes:uiusernotificationtypealert| uiusernotificationtypebadge| Uiusernotificationtypesound Categories:nil]];

//}

return YES;

}

#pragma mark executes after the user registration notification method has been called (that is, after the Registerusernotificationsettings: method is executed)

-(void) Application: (UIApplication *) application didregisterusernotificationsettings: (Uiusernotificationsettings * ) notificationsettings{

if (Notificationsettings.types!=uiusernotificationtypenone) {

[Self addlocalnotification];

}

}

#pragma mark enters the foreground to set up message information

-(void) Applicationwillenterforeground: (uiapplication *) application{

[[UIApplication sharedapplication]setapplicationiconbadgenumber:0];//enter foreground to cancel app message icon

}

#pragma mark-Private method

#pragma mark Add local notifications

-(void) addlocalnotification{

Defining local Notification objects

Uilocalnotification *notification=[[uilocalnotification Alloc]init];

Set call time

Notification.firedate=[nsdate datewithtimeintervalsincenow:10.0];//notification trigger time, after 10s

notification.repeatinterval=2;//Notification Repeat Count

Notification.repeatcalendar=[nscalendar currentcalendar];//The current calendar, it is best to set the time zone and other information to enable automatic synchronization

Set notification properties

[Email protected] "Recently added a lot of interesting features, whether to experience now?" "; Notification body

notification.applicationiconbadgenumber=1;//the number of messages displayed in the upper-right corner of the application icon

[Email protected] "open app"; Sliding action tips for the Standby interface

[Email protected] "Default";//click the notification to open the app when the start picture, here to use the program to start the picture

notification.soundname=uilocalnotificationdefaultsoundname;//The sound that is played when the notification is received, the default message sound

[Email protected] "MSG.CAF";//notification sound (requires real machine to hear sound)

Set up user information

[Email protected] {@ "id": @1,@ "User": @ "Kenshin Cui"};//Other additional information bound to the notification

Invoke notification

[[UIApplication sharedapplication] schedulelocalnotification:notification];

}

#pragma mark Remove local notifications and remember to remove them when this notification is not required

-(void) removenotification{

[[UIApplication sharedapplication] cancelalllocalnotifications];

}

iOS development-Local push 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.