【发布时间】:2015-06-01 06:33:59
【问题描述】:
您好朋友,我是推送通知的新手,请帮帮我。 我正在尝试获取我的应用程序的徽章计数,但“NSDictionary userInfo”仅包含 alert 和 sound 。 这是注册设备推送通知的代码
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)])
{
UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge |UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
}
现在
当我从服务器收到消息时。
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@"%@",userInfo);
if (application.applicationState == UIApplicationStateActive)
{
/***********code to show alert********/
if (![[[NSString alloc]initWithString:[[userInfo objectForKey:@"aps"] objectForKey: @"alert"]] isEqualToString:@""] && [[NSString alloc]initWithString:[[userInfo objectForKey:@"aps"] objectForKey: @"alert"]]!=nil)
{
NSString *MSG =[[NSString alloc]initWithString:[[userInfo objectForKey:@"aps"] objectForKey: @"alert"]];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Message" message:MSG delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
[alert show];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Message" message:@"Notification Received." delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
[alert show];
}
}
application.applicationIconBadgeNumber =[[[userInfo objectForKey:@"aps"] objectForKey: @"badge"]integerValue];
if (userInfo)
{
NSLog(@"%@",userInfo);
if ([userInfo objectForKey:@"aps"])
{
if([[userInfo objectForKey:@"aps"] objectForKey:@"badgecount"])
{
[UIApplication sharedApplication].applicationIconBadgeNumber = [[[userInfo objectForKey:@"aps"] objectForKey: @"badgecount"] intValue];
}
}
}
}
现在输出是
{ aps =
{
alert = "MAgic 2 SAlon App!";
sound = default;
};
}
我的 php 代码是 `
// Put your device token here (without spaces):
$deviceToken = 'xxxxxxxxx';
// Put your private key's passphrase here:
$passphrase = 'xxxxxx';
// Put your alert message here:
$message = 'xxxxxxx!';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
fclose($fp);`
【问题讨论】:
标签: php ios iphone ios7 push-notification