美文网首页
消息推送

消息推送

作者: mayday2024 | 来源:发表于2015-05-25 22:05 被阅读170次

    本地推送

     @implementation ViewController
                - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
                {
                    
                    /*
                     @property(nonatomic,copy) NSDate *fireDate;  //触发时间
                     @property(nonatomic,copy) NSTimeZone *timeZone; //时区
                     
                     @property(nonatomic) NSCalendarUnit repeatInterval;//重复时间  @property(nonatomic,copy) NSCalendar *repeatCalendar; //重复时间
                     
                     @property(nonatomic,copy) CLRegion *region NS_AVAILABLE_IOS(8_0); //区域
                     @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];
                    
                  
    
                   //创建一个本地通知对象
                    UILocalNotification *local = [[UILocalNotification alloc]init];
                    
                    
                    //8秒之后触发通知
                    local.fireDate = [NSDate dateWithTimeIntervalSinceNow:8];
                    
                    //通知的消息体
                    local.alertBody = @"在吗?";
                    
                    //滑动界面的按钮内容
                    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

                //程序从死到生 就会调用    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;
                }
    
                //前提条件 接收到本地推送就会调用  程序活着 无论前台还是后台  点击通知启动的话的 才算是接收了通知
                - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
                {
                NSLog(@"didReceiveLocalNotification - %@",notification);
                }
    

    远程推送http://blog.csdn.net/shenjie12345678/article/details/41120637

    配置好相关文件 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);
                    //打开特定的界面
                }
    
                //一旦接收到远程通知就会调用   特点: 程序活着
                - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
                {
                    NSLog(@"%@",userInfo);
                }
    

    相关文章

      网友评论

          本文标题:消息推送

          本文链接:https://www.haomeiwen.com/subject/indrqttx.html