为了账号安全,请及时绑定邮箱和手机立即绑定

在前台iOS中的App时获取推送通知

在前台iOS中的App时获取推送通知

扬帆大鱼 2019-08-15 17:03:27
在前台iOS中的App时获取推送通知我在我的应用程序中使用推送通知服务。当应用程序在后台时,我能够在通知屏幕上看到通知(当我们从iOS设备的顶部向下滑动时显示的屏幕)。但是如果应用程序在前台是委托方法- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo正在调用,但通知屏幕中不显示通知。我想在通知屏幕上显示通知,无论应用程序是在后台还是前台。我厌倦了寻找解决方案。任何帮助是极大的赞赏。
查看完整描述

3 回答

?
呼如林

TA贡献1798条经验 获得超3个赞

要在应用程序位于前台时显示横幅消息,请使用以下方法。

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])}

您还必须将您的应用代表注册为通知中心的代表:

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      ...
   }


查看完整回答
反对 回复 2019-08-15
?
肥皂起泡泡

TA贡献1829条经验 获得超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];          
    }    }


查看完整回答
反对 回复 2019-08-15
?
人到中年有点甜

TA贡献1895条经验 获得超7个赞

对于任何可能感兴趣的人,我最终创建了一个自定义视图,看起来像顶部的系统推送横幅,但添加了一个关闭按钮(小蓝色X)和一个选项来点击消息进行自定义操作。它还支持在用户有时间读取/解除旧通知之前到达多个通知的情况(没有限制可以堆积多少...)

链接到GitHub:AGPushNote

用法基本上是在线:

[AGPushNoteView showWithNotificationMessage:@"John Doe sent you a message!"];

它在iOS7上看起来像这样(iOS6具有iOS6的外观和感觉......)


查看完整回答
反对 回复 2019-08-15
  • 3 回答
  • 0 关注
  • 1274 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信