當一個ios 的push訊息到來的時候,有兩種情況,apple為我們提供了2個回撥api來處理:
1.app 未啟動狀態
application:didFinishLaunchingWithOptions
//launchOptions 包含 推送 playload 資訊
if (launchOptions != nil)
{
NSDictionary *dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dictionary != nil)
{
NSLog(@"Launched from push notification: %@", dictionary);
//根據 playload內容處理
[self addMessageFromRemoteNotification:dictionary updateUI:NO];
}
}
2.app 執行狀態
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
NSLog(@"Received notification: %@", userInfo);
[self addMessageFromRemoteNotification:userInfo updateUI:YES];
}
自定義訊息內容
{
"aps": {
"alert": "hello users update .",
"badge": 1,
"sound": "default"
},
"page":"chat"
}
可以在訊息自定義欄位內容,然後在回撥函式裡判斷處理