2017-06-02 87 views
1

我試圖在應用程序的前臺很多得到修改通知.... 通過創建Notification Service Extensions沒有得到推送通知的通知托盤(頂部),而應用程序在前臺的iOS

背景和殺害成功修改,但僅在前臺獲取警報主體中的原始有效負載不在通知中。

這裏 在NotificationService.m文件

@implementation NotificationService 
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler { 
    self.contentHandler = contentHandler; 
    self.bestAttemptContent = [request.content mutableCopy]; 

    // Modify the notification content here... 


    NSLog(@"tesrrrr"); 

    self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title]; 
    self.bestAttemptContent.body = [NSString stringWithFormat:@"%@[ body added Manually ]", self.bestAttemptContent.body]; 



    self.contentHandler(self.bestAttemptContent); 
} 
{(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { 
    NSDictionary *userInfo1 = userInfo; 
    NSLog(@"userInfo: %@", userInfo1); 

    //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) 
    { 
     //opened from a push notification when the app was on background 


    /* UILocalNotification *localNotification = [[UILocalNotification alloc] init]; 
     localNotification.userInfo = userInfo; 
     localNotification.soundName = UILocalNotificationDefaultSoundName; 
     localNotification.alertBody = @"xyz"; 
     localNotification.fireDate = [NSDate date]; 
     [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 

     */ 


     NSLog(@"userInfoUIApplicationStateactive->%@",[userInfo objectForKey:@"aps"]); 
     NSLog(@"userInfoUIApplicationStateactive->%@",[userInfo objectForKey:@"aps"]); 

     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]; 

     // [self scheduleAlarmForDate1:[NSDate date]alarmDict:userInfo]; 



    } 
    else 
    { 
     // a push notification when the app is running. So that you can display an alert and push in any view 

     NSLog(@"userInfoUIApplicationStateBackground->%@",[userInfo objectForKey:@"aps"]); 

    } 
}} 
+0

在IOS 10你有一個委託方法來處理這種情況,在以前的版本中,你必須處理,在''didReceiveRemoteNotification –

+0

通知完全依賴於有效載荷數據及其使用的密鑰,您是否已經在'project> target> capabilities> Background Modes'下啓用了'remote notification'服務(這使應用程序能夠在後臺獲得通知)。 – vaibhav

回答

1

實施UNUserNotificationCenterDelegate委託方法得到通知(盤在頂部),而應用程序是在前臺。但它只會與IOS 10

工作在你didFinishLaunchingWithOptions方法設置UNUserNotificationCenterDelegate委託這個樣子。

[UNUserNotificationCenter currentNotificationCenter].delegate = self; 

實現委託方法...

//Called when a notification is delivered to a foreground app. 
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{ 

    completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge); 
} 

//Called to let your app know which action was selected by the user for a given notification. 
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{ 

    completionHandler(); 
} 

注意

如果您的應用開發目標是小於IOS10使用此設置委託。

#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 
    // For iOS 10 display notification (sent via APNS) 
    [UNUserNotificationCenter currentNotificationCenter].delegate = self; 
#endif 
+0

感謝它爲我工作 – SANTOSH

0

當應用程序處於前臺時,頂部的通知橫幅未顯示。該橫幅由操作系統管理,並且只調用時,應用程序在後臺或終止狀態。

雖然這不適用於iOS 10,但是從iOS 10開始,您可以執行此操作, 您需要捕獲委託中的通知並調用顯示橫幅的函數。

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
completionHandler(UNNotificationPresentationOptions.alert) 
} 

但正如我所說,這隻適用於iOS 10+。對於以前的兼容性,你可以做的是提到如下:

爲它創建一個自定義的控制。 您可以輕鬆地趕上委託函數的通知時,應用程序是在前臺。然後你可以調用你自己的自定義控件(一個視圖)來顯示一個橫幅。 幸運的是,有很多不錯的通知自定義控件做到這一點。 這裏有幾個:

1. BRYXBanner

2. CRToast

+0

是的,我正在編輯答案,我認爲OP想要一個更通用的解決方案 –

相關問題