在前臺iOS中的App時獲取推送通知我在我的應(yīng)用程序中使用推送通知服務(wù)。當應(yīng)用程序在后臺時,我能夠在通知屏幕上看到通知(當我們從iOS設(shè)備的頂部向下滑動時顯示的屏幕)。但是如果應(yīng)用程序在前臺是委托方法- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo正在調(diào)用,但通知屏幕中不顯示通知。我想在通知屏幕上顯示通知,無論應(yīng)用程序是在后臺還是前臺。我厭倦了尋找解決方案。任何幫助是極大的贊賞。
3 回答

呼如林
TA貢獻1798條經(jīng)驗 獲得超3個贊
要在應(yīng)用程序位于前臺時顯示橫幅消息,請使用以下方法。
iOS 10,Swift 3/4:
// This method will be called when app received push notifications in foregroundfunc userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { completionHandler([.alert, .badge, .sound])}
iOS 10,Swift 2.3:
@available(iOS 10.0, *)func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void){ //Handle the notification completionHandler( [UNNotificationPresentationOptions.Alert, UNNotificationPresentationOptions.Sound, UNNotificationPresentationOptions.Badge])}
您還必須將您的應(yīng)用代表注冊為通知中心的代表:
import UserNotifications// snip!class AppDelegate : UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate// snip! func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // set the delegate in didFinishLaunchingWithOptions UNUserNotificationCenter.current().delegate = self ... }

肥皂起泡泡
TA貢獻1829條經(jīng)驗 獲得超6個贊
下面的代碼將適合您:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { application.applicationIconBadgeNumber = 0; //self.textView.text = [userInfo description]; // We can determine whether an application is launched as a result of the user tapping the action // button or whether the notification was delivered to the already-running application by examining // the application state. if (application.applicationState == UIApplicationStateActive) { // Nothing to do if applicationState is Inactive, the iOS already displayed an alert view. UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Did receive a Remote Notification" message:[NSString stringWithFormat:@"Your App name received this notification while it was running:\n%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]]delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show]; } }

人到中年有點甜
TA貢獻1895條經(jīng)驗 獲得超7個贊
對于任何可能感興趣的人,我最終創(chuàng)建了一個自定義視圖,看起來像頂部的系統(tǒng)推送橫幅,但添加了一個關(guān)閉按鈕(小藍色X)和一個選項來點擊消息進行自定義操作。它還支持在用戶有時間讀取/解除舊通知之前到達多個通知的情況(沒有限制可以堆積多少...)
鏈接到GitHub:AGPushNote
用法基本上是在線:
[AGPushNoteView showWithNotificationMessage:@"John Doe sent you a message!"];
它在iOS7上看起來像這樣(iOS6具有iOS6的外觀和感覺......)
- 3 回答
- 0 關(guān)注
- 1403 瀏覽
添加回答
舉報
0/150
提交
取消