[英]Objective-C IOS 7 push notification save data when app not running?
我用推送通知設置創建了ios應用程序,每個人都可以在我的應用程序中與其他人聊天,我將解決一些問題:
1)解決:當應用程序未激活但仍在后台運行(不是全部退出)時,我可以處理推送通知並保存到數據庫(SQLite)代碼:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
//some code here
}
2)解決:當用戶完成退出我的應用程序,新消息incomming和用戶點擊通知氣球和用戶可以閱讀新消息,為此我將使用此代碼:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
/* this code working when application not running */
NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
if(remoteNotif)
{
[self handleRemoteNotification:application userInfo:remoteNotif];
return YES;
}
return YES;
}
3沒解決,我的問題需要幫助
請幫忙
如果您的應用程序未運行且用戶單擊推送通知以打開它,您可以在application:didFinishLaunchingWithOptions
獲取有關此通知的數據application:didFinishLaunchingWithOptions
方法。
根據Apple文檔 :
如果您的應用程序不在最前面或未運行,則通過檢查傳遞給應用程序的選項字典來處理通知:didFinishLaunchingWithOptions:您的應用程序委托的UIApplicationLaunchOptionsLocalNotificationKey或UIApplicationLaunchOptionsRemoteNotificationKey鍵。
但是,如果在收到通知時應用程序沒有運行,並且通過單擊此通知從未打開應用程序,則無法獲取其數據。
這與推送通知僅為最佳努力(參見下面的注釋)這一事實告訴我們,我們應該總是有另一種方法來獲取信息(就像Chetan所說的那樣)。 所以永遠不要只依靠推動 。
重要提示:發送通知是“盡力而為”,不能保證。 它不是為了向您的應用提供數據,而是為了通知用戶有新數據可用。
如果仍然沒有解決,請回答你的3子彈。 iOS 7.0及更高版本中有一種方法可以發送通知並訪問其內容,而無需用戶點擊它。 因此,您發送的通知有效內容具有一個名為content-available的參數。 您可以在以下鏈接中查看有效負載parmaters: https : //developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html
因此,當您將內容設置為1並發送通知時,即使應用程序不在后台或前台(必須啟用遠程通知),iOS也會調用以下功能
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
然后,您可以使用userInfo字典來獲取通知數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.