iOS开发之判断用户是否打开APP通知开关


一.前言

  在多数移动应用中任何时候都只能有一个应用程序处于活跃状态,如果其他应用此刻发生了一些用户感兴趣的那么通过通知机制就可以告诉用户此时发生的事情。iOS中通知机制又叫消息机制,其包括两类:一类是本地通知;另一类是推送通知,也叫远程通知。两种通知在iOS中的表现一致,可以通过横幅或者弹出提醒两种形式告诉用户,并且点击通知可以会打开应用程序,但是实现原理却完全不同。

 

二.代码如下

@interface AppDelegate ()

 

@end

 

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

   //开启通知

    if ([[UIApplication sharedApplication]currentUserNotificationSettings].types!=UIUserNotificationTypeNone) {

        [self addLocalNotification];

    }else{

        [[UIApplication sharedApplication]registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound  categories:nil]];

    }

    

    return YES;

}

 

#pragma mark 添加本地通知

-(void)addLocalNotification{

  //定义本地通知对象

    UILocalNotification *notification=[[UILocalNotification alloc]init];

  //调用通知

    [[UIApplication sharedApplication] scheduleLocalNotification:notification];

}

 

#pragma mark 移除本地通知,在不需要此通知时记得移除

-(void)removeNotification{

    [[UIApplication sharedApplication] cancelAllLocalNotifications];

}

@end

 

三.效果图

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM