【问题标题】:Add message to Remote Push Notification in Objective C (iOS)在Objective C(iOS)中向远程推送通知添加消息
【发布时间】:2018-01-18 06:40:41
【问题描述】:

如何从动态通知服务中获取并显示推送通知中的消息

push: {
    aps =  {
        alert = "My First Notification";
        sound = default;
        Msg = {
            myData = (
                      {
                        Msg = "Awesome";
                        Id = 123;
                        Date = "Jan 18 2018";
                       }
                      );
              };
           };
       }

因为我想在推送通知到达时显示 alert = "My First Notification" 和 Msg = "Awesome"。我不知道如何获取和显示。请帮我解决这个问题。 TIA

我试过下面的代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

    [application registerForRemoteNotifications];


    return YES;
}

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    NSLog(@"My token is: %@", deviceToken);

    NSString *deviceTokenStr = [[[[deviceToken description]
                                  stringByReplacingOccurrencesOfString: @"<" withString: @""]
                                 stringByReplacingOccurrencesOfString: @">" withString: @""]
                                stringByReplacingOccurrencesOfString: @" " withString: @""];

    NSLog(@"Device Token: %@", deviceTokenStr);
}

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
    NSLog(@"Failed to get token, error: %@", error);
}

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
    NSLog(@"Received notification: %@", userInfo);
    NSUserDefaults * loginDefaults = [NSUserDefaults standardUserDefaults];

    UIApplicationState state = [[UIApplication sharedApplication] applicationState];
    if (UIApplicationStateActive == state )
    {

        return;
    }

}

我只想在推送通知中显示它。

【问题讨论】:

  • 你在 userInfo 中得到了什么?
  • push: { aps = { alert = "我的第一个通知";声音=默认; Msg = { myData = ( { Msg = "Awesome"; Id = 123; Date = "Jan 18 2018"; } ); }; }; }
  • 那有什么问题,所有数据都在那里,只是得到它
  • 解析 userInfo 中的数据并在 Alert 视图中设置 Title 和 message
  • 下面是获取数据的答案,现在创建警报并显示它。如果不起作用,请尝试编写一些代码让我们知道

标签: ios objective-c xcode apple-push-notifications objective-c-2.0


【解决方案1】:

据我了解,您希望您的推送在通知屏幕上显示标题My First Notification 和消息(或正文)Awesome。为此,您无需在应用程序中编写代码。真正发挥作用的是有效载荷的结构。

目前您正在以

的形式发送有效负载
{
  "aps": {
    "alert": "My First Notification",
    "sound": "default",
    "Msg": {
      "myData": [
        {
          "Msg": "Awesome",
          "Id": 123,
          "Date": "Jan 18 2018"
        }
      ]
    }
  }
}

根据apple docs,通知屏幕上不会显示Awesome消息

要在通知屏幕上显示Awesome 消息,您需要指定body 键。创建如下所示的有效负载并发送此推送,您将在通知屏幕上看到标题和正文。还可以通过上述文档了解更多信息

{
  "aps": {
    "alert": {
      "title": "My First Notification",
      "body": "Awesome",
      "sound": "default"
    }
  },
  "myData": {
    "Id": 123,
    "Date": "Jan 18 2018"
  }
}

最后我想补充一点,在 android 中我们需要在应用程序中编写代码来显示推送通知,而在 ios 中我们不必编写代码或更准确地说我们的应用程序不需要控制推送的显示方式。这完全取决于使用适当密钥向 APNS 发送数据的服务器

【讨论】:

    【解决方案2】:

    首先UserInfoNSDictionary,所以你可以通过键获取所有需要的值,你的情况是这样的

    代码

    NSDictionary * aps = [userInfo objectForKey:@"aps"];
    NSDictionary * myData = [aps objectForKey:@"Msg"];
    //inside of Msg dictionary you have an array of dictionaries
    NSArray * arrayOfData = [myData objectForKey:@"myData"];
    NSLog(@"%@",[arrayOfData objectAtIndex:0]);
    NSDictionary * infoDict = [arrayOfData objectAtIndex:0];
    //here we get the message!!!
    NSString* message = [infoDict objectForKey:@"Msg"];
    //then show your alert
    UIAlertController* alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"My First Notification", nil) message:message preferredStyle:UIAlertControllerStyleAlert];
    [[[self window] rootViewController] presentViewController:alertController animated:true completion:nil];
    

    此代码未经测试,如果有问题,请告诉我,但这就是想法

    【讨论】:

    • 我认为他是初学者,所以你可以添加NSString *awesomeMsg=[[arrMyData objectAtIndex:0] valueForKey:@"Msg"]; // get your Msg - Awesome 这一行,这样他也可以得到消息。
    • 再次检查我的答案@NiravKotecha,即使它现在显示警报,请告诉我
    • 消息是显示在推送通知中还是在 AlertView 中。我只想在推送通知中显示它
    • @Rajeev 我不明白你需要什么,如果你想在你的应用中显示推送通知的消息,你需要使用 UIAlertView
    • 我不想在 AlertView 中显示推送通知的消息。我想在推送通知中显示消息
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-15
    • 2015-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多