1.准备工作developer.apple.com里面完成

前提有 开发证书iosdev.cer(iOS Development发布用到证书iosdist.cer(iOS Distribution)

在Identifiers页面点+,添加bundle ID ,例如com.xxx.yyy”,在配置页面下拉到Push Notifications,勾上,然后右边看到edit链接,点edit弹窗提示添加证书(用keychain生成一份push. certSigningRequest),相应导入certSigningRequest文件,这样就可以得到两个证书aps_development.cer开发用到)和aps.cer(发布用到

在Profiles页面点+,生成两份配置 pushdev开发)和pushappstore发布)其中的identify就选上面那个“com.xxx.yyy对应的项

在Keys页面点+,随便填个名字勾选“Apple Push Notifications service (APNs)”,点continue然后下载保存好,文件名暂定叫push.p8 里面也有一个key ID 假设是“SP12345678”

2. Xcode代码

在AppDelegate.m加入

#import <UserNotifications/UserNotifications.h>

在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法里面注册通知

[UIApplication sharedApplication].applicationIconBadgeNumber=0;
  /*
   identifier行为标识符用于调用代理方法识别是哪种行为。
   title:行为名称uiusernotificationactivationmode:即行为是否打开appauthenticationrequired是否需要解锁destructive:这个决定按钮显示颜色yes的话按钮会是红色。
   behavior:点击按钮文字输入是否弹出键盘
   */
  
  UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"action1" title:@"foreground action" options:UNNotificationActionOptionForeground];
  UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"action2" title:@"authentication required" options:UNNotificationActionOptionAuthenticationRequired];
  
  UNNotificationCategory *category1 = [UNNotificationCategory categoryWithIdentifier:@"category1" actions:@[action2,action1] intentIdentifiers:@[@"action1",@"action2"] options:UNNotificationCategoryOptionCustomDismissAction];
    
  [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObjects:category1, nil]];
  [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError * _Nullable error) {
    NSLog(@"completionhandler");
  }];
  
    
  [[UIApplication sharedApplication] registerForRemoteNotifications];
  
  [UNUserNotificationCenter currentNotificationCenter].delegate = self;

再添加两个相应函数

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *) error {
    NSLog(@"Registfail%@",error);
}
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
    NSLog(@"%@",deviceToken);//这里的Token就是我们设备要告诉服务端的Token码,
		unsigned char buffer[1024]={0};
		NSMutableString *mstr = [NSMutableString string];
  for (int i=0; i<deviceToken.length; i++) {
    unsigned char chr = buffer[i];
    [mstr appendFormat:@"%02x",chr];
  }
  NSLog(@"mstr=%@",mstr);  //这个字符串才是需要保存服务器上面的
	//TODO:发送deviceToken公司个人)PHP服务器
}

3. PHP服务器实现

https://api.sandbox.push.apple.com/3/device/{device-token}  ||dev 开发

https://api.push.apple.com/3/device/{device-token}   || pub 发布

添加 header:

content-type: application/json
apns-topic: com.wancaiinfo.yunzhan365app
apns-priority: 10
apns-push-type: alert
authorization: bearer $jwt

   //badge = 6;   //显示icon里面数值

body:

{“aps":{"alert":{"title":"title test","body":"content test"},"badge":6,"sound":"default"}}

其中$jwt = $header.$claims.$signCode

$header=Base64(‘{ “alg”: “ES256”, “kid“: “SP12345678(上面提到的key ID)” }’) 结果替换+为-,/为_;   暂时定义这个函数名JWTBASE64。

$claims = JWTBASE64(‘{ “iss“: “(teamId 开发组ID,自己developer.apple.com上找)”, “iat”: (时间戳)}’);

$signCode= JWTBASE64(sign($header.$claims));  签名后的数据还是要JWTBASE64一把。

sign签名函数可以使用JWT库做到,目前使用加密方法是ES256

4.手动测试(将下面的代码改好,保存为push_test,运行 “chomd +x push_test”加运行权限

#!/bin/bash

# Get curl with HTTP/2 and openssl with ECDSA: 'brew install curl-openssl'
curl=/usr/bin/curl
openssl=/opt/local/bin/openssl

# --------------------------------------------------------------------------
deviceToken={这里填写在-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken拿到的deviceToken字符串}

authKey="./push.p8"   #(p8文件authKeyId=SP12345678 #(key ID)
teamId=Sxxxxxxxxx  # (开发组ID)
bundleId=com.xxx.yyyy  #(Bundle ID)
#endpoint=https://api.development.push.apple.com
endpoint=https://api.sandbox.push.apple.com

read -r -d '' payload <<-'EOF'
{
   "aps": {
      "alert": {
         "title": " test for title",
         "body": "testing ...."
      }
   }
}
EOF

# --------------------------------------------------------------------------

base64() {
   $openssl base64 -e -A | tr -- '+/' '-_' | tr -d =
}

sign() {
   printf "$1"| $openssl dgst -binary -sha256 -sign "$authKey" | base64
}

time=$(date +%s)
header=$(printf '{ "alg": "ES256", "kid": "%s" }' "$authKeyId" | base64)
claims=$(printf '{ "iss": "%s", "iat": %d }' "$teamId" "$time" | base64)
jwt="$header.$claims.$(sign $header.$claims)"

$curl --verbose 
   --header "content-type: application/json" 
   --header "authorization: bearer $jwt" 
   --header "apns-topic: $bundleId" 
   --header "apns-push-type: alert" 
   --data "$payload" 
   $endpoint/3/device/$deviceToken

//—————-后续—————–

真机测试遇到

You've implemented -[<UIApplicationDelegate> application:didReceiveRemoteNotification:fetchCompletionHandler:], but you still need to add "remote-notification" to the list of your supported UIBackgroundModes in your Info.plist.

这个应该是要在项目工程里面“Signing &amp;Capabilities”点+右边的“Capability”,选择“Background Modes”,然后勾上”Remote notifications”.

原文地址:https://blog.csdn.net/ftpleopard/article/details/122060128

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_36568.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注