├── apns.jar ├── .gitignore ├── IOSPush.java ├── LICENSE ├── README.md └── AndroidPush.java /apns.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Big-Silver/Ionic-PushNotification-Server/HEAD/apns.jar -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ### Java template 2 | *.class 3 | 4 | # Mobile Tools for Java (J2ME) 5 | .mtj.tmp/ 6 | 7 | # Package Files # 8 | *.war 9 | *.ear 10 | 11 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 12 | hs_err_pid* 13 | ### Maven template 14 | target/ 15 | pom.xml.tag 16 | pom.xml.releaseBackup 17 | pom.xml.versionsBackup 18 | pom.xml.next 19 | release.properties 20 | dependency-reduced-pom.xml 21 | buildNumber.properties 22 | .mvn/timing.properties 23 | 24 | .idea/ 25 | *.iml -------------------------------------------------------------------------------- /IOSPush.java: -------------------------------------------------------------------------------- 1 | import com.notnoop.apns.APNS; 2 | import com.notnoop.apns.ApnsService; 3 | 4 | public class IOSPush { 5 | private static String DEVICE_TOKEN = "YOUR_DEVICE_TOKEN"; 6 | private static String PATH_TO_P12_CERT = "YOUR_P12_CERTIFICATE_PATH"; 7 | private static String CERT_PASSWORD = "YOUR_P12_CERTIFICATE_PASSWORD"; 8 | 9 | public static void main(String[] args) { 10 | ApnsService service = 11 | APNS.newService() 12 | .withCert(PATH_TO_P12_CERT, CERT_PASSWORD) 13 | .withSandboxDestination() 14 | .build(); 15 | 16 | String payload = APNS.newPayload() 17 | .alertBody("My first notification\nHello, I'm push notification") 18 | .sound("default") 19 | .build(); 20 | 21 | service.push(DEVICE_TOKEN, payload); 22 | System.out.println("The message has been hopefully sent..."); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Ankush Aggarwal 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Push Notification Server 2 | ======================== 3 | 4 | This can be used to send Push Notification to iOS or Android Device. 5 | 6 | ### Android Push Notification 7 | 8 | * ```SERVER_KEY``` - if you don't have SERVER_KEY, generate using [this tutorial](https://medium.com/@ankushaggarwal/gcm-setup-for-android-push-notifications-656cfdd8adbd) :tada: 9 | * ```DEVICE_TOKEN``` - if you don't have device token, in case of Ionic 2, use [this tutorial](https://medium.com/@ankushaggarwal/push-notifications-in-ionic-2-658461108c59) or [this repo](https://github.com/Big-Silver/Ionic-PushNotification) :+1: 10 | * ```AndroidPush.java``` - run this file to push notification to Android device 11 | 12 | ``` 13 | $ javac AndroidPush.java 14 | $ java AndroidPush 15 | ``` 16 | 17 | ### iOS Push Notification 18 | 19 | * ```APNS certificate(.p12)``` - if you don't have .p12 certificate, generate using [this tutorial](https://medium.com/@ankushaggarwal/generate-apns-certificate-for-ios-push-notifications-85e4a917d522) :tada: 20 | * ```DEVICE_TOKEN``` - if you don't have device token, in case of Ionic 2, use [this tutorial](https://medium.com/@ankushaggarwal/push-notifications-in-ionic-2-658461108c59) or [this repo](https://github.com/Big-Silver/Ionic-PushNotification) :+1: 21 | * ```IOSPush.java``` - run this file to push notification to iOS device 22 | 23 | ``` 24 | $ javac -cp apns.jar:. IOSPush.java 25 | $ java -cp apns.jar:. IOSPush 26 | ``` 27 | -------------------------------------------------------------------------------- /AndroidPush.java: -------------------------------------------------------------------------------- 1 | import java.io.OutputStream; 2 | import java.net.HttpURLConnection; 3 | import java.net.URL; 4 | 5 | public class AndroidPush { 6 | 7 | private static String SERVER_KEY = "YOUR_SERVER_KEY"; 8 | private static String DEVICE_TOKEN = "YOUR_DEVICE_TOKEN"; 9 | 10 | 11 | /** 12 | * USE THIS METHOD to send push notification 13 | */ 14 | public static void main(String[] args) throws Exception { 15 | String title = "My First Notification"; 16 | String message = "Hello, I'm push notification"; 17 | sendPushNotification(title, message); 18 | } 19 | 20 | 21 | /** 22 | * Sends notification to mobile, YOU DON'T NEED TO UNDERSTAND THIS METHOD 23 | */ 24 | private static void sendPushNotification(String title, String message) throws Exception { 25 | String pushMessage = "{\"data\":{\"title\":\"" + 26 | title + 27 | "\",\"message\":\"" + 28 | message + 29 | "\"},\"to\":\"" + 30 | DEVICE_TOKEN + 31 | "\"}"; 32 | // Create connection to send FCM Message request. 33 | URL url = new URL("https://fcm.googleapis.com/fcm/send"); 34 | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 35 | conn.setRequestProperty("Authorization", "key=" + SERVER_KEY); 36 | conn.setRequestProperty("Content-Type", "application/json"); 37 | conn.setRequestMethod("POST"); 38 | conn.setDoOutput(true); 39 | 40 | // Send FCM message content. 41 | OutputStream outputStream = conn.getOutputStream(); 42 | outputStream.write(pushMessage.getBytes()); 43 | 44 | System.out.println(conn.getResponseCode()); 45 | System.out.println(conn.getResponseMessage()); 46 | } 47 | } 48 | --------------------------------------------------------------------------------