本地推送
@implementation ViewController
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
/*
@property(nonatomic,copy) NSDate *fireDate; //觸發(fā)時間
@property(nonatomic,copy) NSTimeZone *timeZone; //時區(qū)
@property(nonatomic) NSCalendarUnit repeatInterval;//重復時間 @property(nonatomic,copy) NSCalendar *repeatCalendar; //重復時間
@property(nonatomic,copy) CLRegion *region NS_AVAILABLE_IOS(8_0); //區(qū)域
@property(nonatomic,assign) BOOL regionTriggersOnce NS_AVAILABLE_IOS(8_0);
// alerts
@property(nonatomic,copy) NSString *alertBody; 消息體
@property(nonatomic) BOOL hasAction;
@property(nonatomic,copy) NSString *alertAction;滑動提示
@property(nonatomic,copy) NSString *alertLaunchImage; 啟動圖片
// sound
@property(nonatomic,copy) NSString *soundName; 提示音 UILocalNotificationDefaultSoundName
// badge
@property(nonatomic) NSInteger applicationIconBadgeNumber; 圖標文字
// user info
@property(nonatomic,copy) NSDictionary *userInfo; 通知字典
[UIUserNotificationSettings settingsForUserNotificationTypes:userNotificationActionSettings:]
@property (nonatomic, copy) NSString *category NS_AVAILABLE_IOS(8_0);
*/
UIUserNotificationSettings * setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
//創(chuàng)建一個本地通知對象
UILocalNotification *local = [[UILocalNotification alloc]init];
//8秒之后觸發(fā)通知
local.fireDate = [NSDate dateWithTimeIntervalSinceNow:8];
//通知的消息體
local.alertBody = @"在嗎?";
//滑動界面的按鈕內(nèi)容
local.alertAction = @"滑動開始聊天";
//啟動圖片
local.alertLaunchImage = @"refgdrtrdg";
//接收到本地通知的時候的提示音
local.soundName = UILocalNotificationDefaultSoundName;
// local.applicationIconBadgeNumber = 9999999;
local.userInfo = @{@"msg" : @"女神:在嗎?"};
// // 應用級別的事情 這個設置了會崩潰IOS7?哥桥?
// [[UIApplication sharedApplication]registerUserNotificationSettings:setting];
//定制一個本地通知
[[UIApplication sharedApplication]scheduleLocalNotification:local];
}
- (void)viewDidLoad {
[super viewDidLoad];
}
@end
@implementation AppDelegate
//程序從死到生 就會調(diào)用 1> 點擊圖標 2>點擊通知
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//獲得本地通知的字典
//UIApplicationLaunchOptionsLocalNotificationKey 獲得本地通知的key
NSDictionary *locainfo = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
if (locainfo) { //接收到本地通知2>點擊通知
UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 300)];
lable.numberOfLines = 0;
lable.backgroundColor = [UIColor redColor];
lable.text = [NSString stringWithFormat:@"%@",locainfo];
//添加一個控件到主視圖的View
[self.window.rootViewController.view addSubview:lable];
}
return YES;
}
//前提條件 接收到本地推送就會調(diào)用 程序活著 無論前臺還是后臺 點擊通知啟動的話的 才算是接收了通知
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(@"didReceiveLocalNotification - %@",notification);
}
遠程推送http://blog.csdn.net/shenjie12345678/article/details/41120637
配置好相關(guān)文件 pushMEbaby
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSDictionary *dict = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
if (dict) {
NSLog(@"點擊了遠程通知 進入的程序");
//打開特定的界面
UILabel *labe = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
labe.text = [NSString stringWithFormat:@"%@",dict];
labe.numberOfLines = 0;
labe.backgroundColor = [UIColor redColor];
[self.window.rootViewController.view addSubview:labe];
}
//1.注冊遠程通知
UIUserNotificationSettings *setings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:nil];
//注冊通知類型
[[UIApplication sharedApplication]registerUserNotificationSettings:setings];
//遠程通知 (拿到 UUID bundle ID 傳給蘋果)
[[UIApplication sharedApplication]registerForRemoteNotifications];
return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
//應該在這個地方保存 deviceToken
NSLog(@"%@",deviceToken);
//打開特定的界面
}
//一旦接收到遠程通知就會調(diào)用 特點: 程序活著
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@"%@",userInfo);
}