本地推送:
UILocalNotification *notification=[[UILocalNotification alloc] init];
if (notification!=nil) {
NSDate *now=[NSDate new];
notification.fireDate=[now dateByAddingTimeInterval:60];//60秒後通知
notification.repeatInterval=0;//循環次數
notification.timeZone=[NSTimeZone defaultTimeZone];
notification.applicationIconBadgeNumber=1; //應用的紅色數字
notification.soundName= UILocalNotificationDefaultSoundName;//聲音
notification.alertBody=@"通知內容";//提示信息 彈出提示框
notification.alertAction = @"打開"; //提示框按鈕
//notification.hasAction = NO; //是否顯示額外的按鈕,爲no時alertAction消失
// NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];
//notification.userInfo = infoDict; //添加額外的信息
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
推送過後,如果應用處於後臺狀態,可實現代理方法來進行想要的操作
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
}
如果應用已退出,這時候要在下面的方法中來取出推送並處理
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification * push=[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];//取出推送對象
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
遠程推送的大致流程如下:
1.使用appId註冊推送服務
2.獲得推送用的token
3.app上傳token到自己的服務器
4.自己的服務器將推送信息和token發送給apns
5.apns進行推送
示意圖: