设为首页 加入收藏

TOP

iOS开发之判断用户是否打开APP通知开关
2017-10-11 14:59:50 】 浏览:1957
Tags: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

 

三.效果图

 

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Linux Shell——bash shell 脚本.. 下一篇2016款MACBOOK PRO触控条版 安装W..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目