- (IBAction)setReminder:(id)sender {
//这是iOS 8.0中必须要获取的权限问题
NSDate *now = [NSDatedate];
if ([[[UIDevicecurrentDevice] systemVersion]floatValue] >= 8.0) {
UIUserNotificationType myTypes =UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert |UIRemoteNotificationTypeSound;
UIUserNotificationSettings *settings = [UIUserNotificationSettingssettingsForTypes:myTypescategories:nil];
[[UIApplicationsharedApplication] registerUserNotificationSettings:settings];
}else
{
UIRemoteNotificationType myTypes =UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound;
[[UIApplicationsharedApplication] registerForRemoteNotificationTypes:myTypes];
}
UILocalNotification *reminderNotification = [[UILocalNotificationalloc] init];
//when notification should fire
[reminderNotificationsetFireDate:[now dateByAddingTimeInterval:20]];
[reminderNotificationsetTimeZone:[NSTimeZonedefaultTimeZone]];
//what notification should look like
[reminderNotificationsetAlertBody:@"Don't forget to Shout Out!"];
[reminderNotificationsetAlertAction:@"Shout Now"];
//[reminderNotification setSoundName:UILocalNotificationDefaultSoundName];
[reminderNotificationsetApplicationIconBadgeNumber:1];
//schedule notification
[[UIApplicationsharedApplication]
scheduleLocalNotification:reminderNotification];
UIAlertView *successAlert = [[UIAlertViewalloc] initWithTitle:@"Reminder"
message:@"Your Reminder has been Scheduled"
delegate:nil
cancelButtonTitle:@"OK Thanks!"
otherButtonTitles:nil];
[successAlertshow];
}
这个Demo中有生成本地推送的全部信息。当点击提醒信息的时候会触发的事在Appdelegate.m中的方法。这时候就是要处理徽章清零的时候了。两种情况下要清零,第一种是从Notification中点进去清零,还有一种是自己点进app,试app编程Activity的时候。
这是第一种。注意:这里接受本地和远程的方法不一样!!!
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notif
{
application.applicationIconBadgeNumber =0;
if ([applicationapplicationState] == UIApplicationStateActive) {
NSLog(@"Received local notification - app active");
}else {
NSLog(@"Received local notification - from background");
}
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
application.applicationIconBadgeNumber =0;
if ([applicationapplicationState] == UIApplicationStateActive) {
NSLog(@"Received remote notification - app active");
}else {
NSLog(@"Received remote notification - from background");
}
}
这是第二种。
- (void)applicationDidBecomeActive:(UIApplication *)application {
[[UIApplicationsharedApplication] setApplicationIconBadgeNumber:0];
}
完成徽章清零,还要处理跳转界面的问题。那么就是在上述的对应的代码里面添加需要的操作
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notif
{
if ([application applicationState] == UIApplicationStateActive) {
NSLog(@"Received local notification - app active");
} else {
NSLog(@"Received local notification - from background");
self.myEditor = [[EditorViewController alloc] initWithNibName:@"EditorViewController" bundle:nil];
[window addSubview:myEditor.view];
[window makeKeyAndVisible];
}
}