2018年3月17日 星期六

Java apns for iOS + .p12檔案申請及產生

一、申請.p12:

首先要準備一個iOS 開發者帳號,後來進Apple developer登入,請到Certificates, Identifiers & Profiles->Identifiers->App IDs


點Edit,移到Push Notifications
1. Development SSL Certificate(開發時用)
2. Production SSL Certificate(上線後用)
在點Create Certificate...


這裡我們要先產生CertificateSigningRequest.certSigningRequest
去Launchpad 找 Key Access,點開,新增完存在你要存的地方




在回到Apple Developer上傳CertificateSigningRequest.certSigningRequest,在下載aps_development.cer,這邊我是選擇 Development

下載完後aps_development.cer點兩下,去Key Access,產生.p12檔(這個檔還不是push server要用),並將檔案輸出,密碼請記下來,這邊我把檔案命名deveploer.p12

目前有三個檔案

請打開終端機,以下三個指令產生檔案,請確認是否有裝openssl
openssl x509 -in aps_development.cer -inform der -out aps.pem
openssl pkcs12 -nocerts -out key.pem -in push.p12
openssl pkcs12 -export -in aps.pem -inkey key.pem -name "push" -out push.p12
就是push server需要的push.p12

二、Swift 4 Code:

以下是需要在你App新增的程式碼AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
    
        let center = UNUserNotificationCenter.current()
        center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
            // Enable or disable features based on authorization.
        }
        application.registerForRemoteNotifications()

        
        return true
    }
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        
        let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
        print(deviceTokenString)
        
        
    }
    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        
        print("i am not available in simulator \(error)")
        
    }


四、Javanps:

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;

import javapns.communication.exceptions.CommunicationException;
import javapns.communication.exceptions.KeystoreException;
import javapns.devices.Device;
import javapns.devices.exceptions.InvalidDeviceTokenFormatException;
import javapns.devices.implementations.basic.BasicDevice;
import javapns.notification.AppleNotificationServerBasicImpl;
import javapns.notification.PushNotificationManager;
import javapns.notification.PushNotificationPayload;
import javapns.notification.PushedNotification;

public class Main {
 
 public static void main(String[] args) throws JSONException, CommunicationException, KeystoreException, InvalidDeviceTokenFormatException {
  int badge = 1; 
        String sound = "default";
        String msgCertificatePassword = "123456";
                            //90fb73e94659a1822caa51ca079734de6a7e60e44f260a1bfd1326bb4648d734
        String deviceToken = "31A7DA03C54A9AAE77E9E0CA01F4B138ABDFDE9712C2FC56738AD92E614B90FE"; 
        String message = "test push message to ios device11111111111";

        List tokens = new ArrayList();
        tokens.add(deviceToken);

        String certificatePath = "C:\\\\push.p12";
        boolean sendCount = true;

        PushNotificationPayload payload = new PushNotificationPayload();
        payload.addAlert(message); 
        payload.addBadge(badge);


        //payload.addCustomAlertBody(msgEX);
        if (null == sound || "".equals(sound)) {
            payload.addSound(sound);
        }

        PushNotificationManager pushManager = new PushNotificationManager();
        pushManager.initializeConnection(new AppleNotificationServerBasicImpl(
                certificatePath, msgCertificatePassword, false));
        List notifications = new ArrayList();

        if (sendCount) {
            Device device = new BasicDevice();
            device.setToken(deviceToken);
            PushedNotification notification = pushManager.sendNotification(
                    device, payload, true);
            notifications.add(notification);
        } else {
            List devices = new ArrayList();
            for (String token : tokens) {
                devices.add(new BasicDevice(token));
            }
            notifications = pushManager.sendNotifications(payload, devices);
        }

        List failedNotification = PushedNotification
                .findFailedNotifications(notifications);
        List successfulNotification = PushedNotification
                .findSuccessfulNotifications(notifications);
        int failed = failedNotification.size();
        int successful = successfulNotification.size();
        System.out.println("zsl=1¡G" + successful);
        System.out.println("zsl=2¡G" + failed);
        pushManager.stopConnection();
        System.out.println("zsl=3");

 }
 
}

2016年8月15日 星期一

Xcode 7.2 安裝CocoaPods

安裝CocoaPods

打開Terminal(終端機),打以下的的指令:
$ sudo gem install cocoapods


1. 用Xcode新增一個專案
2. 用Terminal(終端機),cd .../專案/,打以下的指令
$ pod init
3. 在打上
$ pod install
4. 以上指令跑完後,會新增 專案.xcworkspace,以後就直接開.xcworkspace的檔案

2016年3月7日 星期一

Xcode 7 上架時遇到 憑證無效...

解決方式:
1. 打開Key Chan在 登入 跟 系統 裡面 把 The Apple Worldwide Developer Relations Certification Intermediate Certificate刪除
2. https://developer.apple.com/certificationauthority/AppleWWDRCA.cer 下載後,點兩下安裝
3. Finish.

Xcode 7 上架時遇到 unexpected cfbundleexecutable key ... googleplus.bundle ... 錯誤

解決方式:
在找出你import的googleplus.bundle
1. googleplus.bundle/Info.plist
2. googleplus.bundle/GPPCommonSharedResources.bundle/Info.plist
3. googleplus.bundle/GPPShareboxSharedResources.bundle/Info.plist
請將這三個檔案的Executable file -> string 的值 清除

2016年1月4日 星期一

Ubuntu OpenSSL 更新或安裝

一、確認OpenSSL版本
# openssl version

二、下載你要更新的版本
OpenSSL 下載連結
# wget https://openssl.org/source/openssl-1.0.2d.tar.gz 
# tar -zxvf openssl-1.0.2d.tar.gz -C /usr/local/src

三、安裝
# cd /usr/local/src/openssl-1.0.2d
# ./config
# make
# make install
安裝完後,請移動資料夾
# mv /usr/bin/openssl /root/
# ln -s /usr/local/ssl/bin/openssl /usr/bin/openssl

後來在確認是否有更新版本
# openssl version

Xcode 7 使用google+ SDK遇到錯誤

GoogleOpenSource.framework/GoogleOpenSource(GTLDateTime.o)' does not contain bitcode. You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE), obtain an updated library from the vendor, or disable bitcode for this target. for architecture arm64

我們只要進入 Build Settings -> Build Options 將 Enable Bitcode 改成NO