How to implement local push for ios, compatible with ios8, and compatible with ios8 for ios

Source: Internet
Author: User

How to implement local push for ios, compatible with ios8, and compatible with ios8 for ios

If you want to be compatible with IOS8 for local push in IOS, you must note that the following statements must be used to register ios8 for local push.

[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];

For versions earlier than iOS 8, I will not talk much about it. I will directly go to the code. Create an oc file (icationicationhelper) and declare the relevant methods in icationicationhelper. h as follows:

#import <UIKit/UIKit.h>@interface NotificationHelper:NSObject <UIApplicationDelegate>{}-(void) addNotifiction:(NSString*) firedate keyA:(NSString*)key messageA:(NSString*)message
-(void)removeLocalNotificationByKey:(NSString*)key;
-(void)removeLocalAllNotification;
-(void) registerLocalNotification:(UIApplication*)application;
+(NotificationHelper*) shareInstance;
@end

The implementation method in icationicationhelper. m file is as follows:

# Import "icationicationhelper. h "@ implementation NotificationHelperstatic NotificationHelper * instance; // implementation Singleton + (icationicationhelper *) shareInstance {static dispatch_once_t onceToken; dispatch_once (& onceToken, ^ {instance = [super allocWithZone: NULL] init] ;}); return instance ;}// push processing [Register Message notification]-(void) registerLocalNotification :( UIApplication *) application {application. applicationIconBadgeNumber = 0; // clear the number on the application icon
// Key: Add version control # if _ IPHONE_ OS _VERSION_MAX_ALLOWED> = _ IPHONE_8_0 // The following line must only run under iOS 8. this runtime check prevents // it from running if it doesn't exist (such as running under iOS 7 or earlier ). UIUserNotificationType types = custom | uiusernotiftypetypesound | custom; UIUserNotificationSettings * mySettings = [UIUserNotificationSettings attributes: types categories: nil]; if ([application respondsToSelector: @ selector (registerUserNotificationSettings :)] {[[UIApplication sharedApplication] registerUserNotificationSettings: mySettings] ;}# endif}-(void) addmediaiction :( NSString *) firedate keyA :( NSString *) key messageA :( NSString *) message {NSLog (@ "addNotifiction"); UILocalNotification * localNotification = [[UILocalNotification alloc] init]; NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat: @ "HH: mm: ss"]; NSDate * now = [formatter dateFromString: firedate]; // The time when the notification is triggered.
// If the firedate format is XX: XX, the notification is sent at a fixed time point, if the input format is XX, the notification will be sent after xx seconds from now on. if (now = nil) {NSTimeInterval secs = [firedate doubleValue]; now = [NSDate dateWithTimeIntervalSinceNow: secs];} localNotification. fireDate = now; // set the time zone localNotification. timeZone = [NSTimeZone defaultTimeZone]; // after triggering, localNotification is displayed in the warning box. alertBody = message; localNotification. alertAction = NSLocalizedString (@ "View Details", nil); // The trigger sound (the default system sound selected here) localNotification. soundName = UILocalNotificationDefaultSoundName; // Trigger frequency (repeatInterval is an enumeration value, which can be set to localNotification every minute, hour, day, or year. repeatInterval = kCFCalendarUnitDay
// Number of unread notifications to be displayed on the App icon (when set to 1, when multiple notifications are not read, the system automatically adds 1. If you do not need to display unread notifications, 0 can be set here)
LocalNotification. applicationIconBadgeNumber = 1;
// Set the notification id, which can be used for notification removal or other values. It can be obtained when a notification is triggered.
LocalNotification. userInfo = @ {@ "id": key };
// Register a local notification
[[UIApplication sharedApplication] scheduleLocalNotification: localNotification];
[LocalNotification release];
}
/** RemoveLocalNotificationByKey */-(void) removeLocalNotificationByKey :( NSString *) key {// retrieve all local notifications NSArray * notifications = [UIApplication sharedApplication]. scheduledlocalnotiications; // set the notification id NSString * notificationId = key; // traverse to remove for (UILocalNotification * localNotification in notifications) {// obtain the id of each notification for comparison if ([localNotification. userInfo objectForKey: @ "id"] isEqualToString: icationicationid]) {// remove a notification [[UIApplication sharedApplication] cancelLocalNotification: localNotification] ;}}-(void) removeLocalAllNotification {[[UIApplication sharedApplication] cancelalllocalconfigurications];} @ end

Example:

For example, call the didfinishlaunchingwitexceptions method when the application is started:

[[Icationicationhelper your instance] registerLocalNotification: application];

For registration and version control, you can call it when you need to send notifications:

[[Icationicationhelper your instance] addNotifiction: "18:30:30" keyA: "key" messageA: "You can get your physical strength! "]

Complete. Because the company's mobile game project needs to use local push, while our project is using the quick cocos2d-x engine, front-end uses LUA to write scripts and interfaces. In this case, there is a problem: How to Write friendly interfaces so that lua can call oc to implement push, so that all logic is implemented in lua.

I will talk about it later.

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.