在 Objective C (iOS) 中向远程推送通知添加消息
Add message to Remote Push Notification in Objective C (iOS)
如何从动态通知服务获取和显示推送通知中的消息
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
是一个 NSDictionary
,因此您可以通过按键获取所有需要的值,在您的情况下会是这样的
代码
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];
此代码未经测试如果有问题请告诉我,但这是我的想法
据我从您的评论中了解到,您希望推送在通知屏幕上显示标题 My First Notification
和消息(或 body)Awesome
。为此,您无需在应用程序中编写代码。神奇的是负载的结构。
目前您正在以
的形式发送负载
{
"aps": {
"alert": "My First Notification",
"sound": "default",
"Msg": {
"myData": [
{
"Msg": "Awesome",
"Id": 123,
"Date": "Jan 18 2018"
}
]
}
}
}
不会根据 apple docs
在通知屏幕上显示 Awesome
消息
要在通知屏幕上显示 Awesome
消息,您需要指定 body
键。创建如下所示的有效负载并发送此推送,您将在通知屏幕上看到标题和 body。另请阅读上述文档以获取更多信息
{
"aps": {
"alert": {
"title": "My First Notification",
"body": "Awesome",
"sound": "default"
}
},
"myData": {
"Id": 123,
"Date": "Jan 18 2018"
}
}
最后我想补充一点,在 android 中我们需要在应用程序中编写代码来显示推送通知,而在 ios 中我们不必编写代码或者更准确地说,我们的应用程序不控制推送的显示方式。这完全取决于使用适当的密钥将数据发送到 APNS 的服务器
如何从动态通知服务获取和显示推送通知中的消息
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
是一个 NSDictionary
,因此您可以通过按键获取所有需要的值,在您的情况下会是这样的
代码
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];
此代码未经测试如果有问题请告诉我,但这是我的想法
据我从您的评论中了解到,您希望推送在通知屏幕上显示标题 My First Notification
和消息(或 body)Awesome
。为此,您无需在应用程序中编写代码。神奇的是负载的结构。
目前您正在以
的形式发送负载{
"aps": {
"alert": "My First Notification",
"sound": "default",
"Msg": {
"myData": [
{
"Msg": "Awesome",
"Id": 123,
"Date": "Jan 18 2018"
}
]
}
}
}
不会根据 apple docs
在通知屏幕上显示Awesome
消息
要在通知屏幕上显示 Awesome
消息,您需要指定 body
键。创建如下所示的有效负载并发送此推送,您将在通知屏幕上看到标题和 body。另请阅读上述文档以获取更多信息
{
"aps": {
"alert": {
"title": "My First Notification",
"body": "Awesome",
"sound": "default"
}
},
"myData": {
"Id": 123,
"Date": "Jan 18 2018"
}
}
最后我想补充一点,在 android 中我们需要在应用程序中编写代码来显示推送通知,而在 ios 中我们不必编写代码或者更准确地说,我们的应用程序不控制推送的显示方式。这完全取决于使用适当的密钥将数据发送到 APNS 的服务器