iOS 中推送的使用详解

- (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];

        

        

    }

}

这里测试的是在本地后台的情况下进行的操作。注意使用window的时候一定要声明才能够应用,并不是@property和@synthesize就可以的
self . myEditor  = [[ EditorViewController   alloc initWithNibName : @"EditorViewController"   bundle : nil ];
这个是必须的。而且不能使用viewcontroller中的方法完成跳转,这里使用的时window中添加view的方法(目前只发现这种方法,之后有更好的方法再补充)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值