• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 没有收到推送通知 - ios

[复制链接]
菜鸟教程小白 发表于 2022-12-13 11:39:50 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

在我的应用程序中我试图接收推送通知,我创建了一个证书并将其转换为 .pem 文件以在终端中测试,并将证书复制到服务器,当我发送设备端未收到来自服务器的通知。 我的代码如下

- (BOOL)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary *)launchOptions {
// Override point for customization after application launch.


if ([application respondsToSelectorselector(isRegisteredForRemoteNotifications)])
{
    // iOS 8 Notifications
    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypesUIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

    [application registerForRemoteNotifications];
}
else
{
    // iOS < 8 Notifications
    [application registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
}
}
- (void)applicationUIApplication *)application didRegisterForRemoteNotificationsWithDeviceTokenNSData *)deviceToken
{

NSString *str = [NSString stringWithFormat"%@",deviceToken];

NSString *newString = [str stringByReplacingOccurrencesOfString" " withString""];
newString = [newString stringByReplacingOccurrencesOfString"<" withString""];
newString = [newString stringByReplacingOccurrencesOfString">" withString""];


    NSData *postData = [[NSString stringWithFormat"regId=%@", newString] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat"%d",[postData length]];

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"<myurl>"]]];

    [request setHTTPMethod:@"OST"];

    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];

    [request setHTTPBody:postData];

    NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];

}  }

服务器端代码

 $message = 'Hello';
$badge = 3;
$sound = 'default';
$development = true;

$payload = array();
$payload['aps'] = array('alert' => $message, 'badge' => intval($badge), 'sound' => $sound);
$payload = json_encode($payload);

$apns_url = NULL;
$apns_cert = NULL;
$apns_port = 2195;

if($development)
{
 $apns_url = 'gateway.sandbox.push.apple.com';
$apns_cert = '/path/to/cert/cert-dev.pem';
}
else
{
$apns_url = 'gateway.push.apple.com';
$apns_cert = '/path/to/cert/cert-prod.pem';
}

$stream_context = stream_context_create();
stream_context_set_option($stream_context, 'ssl', 'local_cert', $apns_cert);

$apns = stream_socket_client('ssl://' . $apns_url . ':' .    $apns_port, $error, $error_string, 2, STREAM_CLIENT_CONNECT, $stream_context);

//  You will need to put your device tokens into the $device_tokens array yourself
$device_tokens = array();

foreach($device_tokens as $device_token)
{
$apns_message = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $device_token)) . chr(0) . chr(strlen($payload)) . $payload;
fwrite($apns, $apns_message);
}

@socket_close($apns);
@fclose($apns);



Best Answer-推荐答案


开发者帐户中可能存在一些证书问题,或者可能是您的设备未添加到配置文件中。您可以通过重新创建配置文件来检查它。

关于ios - 没有收到推送通知 - ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33540334/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap