Skip to content

Instantly share code, notes, and snippets.

@komendantaa
Last active December 6, 2019 11:07
Show Gist options
  • Save komendantaa/1bc19f1761f648acd242f1eefb05cc15 to your computer and use it in GitHub Desktop.
Save komendantaa/1bc19f1761f648acd242f1eefb05cc15 to your computer and use it in GitHub Desktop.
ionic push manual
===== INSTALLATION =====
Install the Cordova and Ionic Native plugins:
$ ionic cordova plugin add phonegap-plugin-push
$ npm install --save @ionic-native/push
*docs
https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/
*tested versions:
"cordova-android": "7.1.1", (required > 7.1)
"@ionic-native/push": "^4.12.2",
"phonegap-plugin-push": "^2.2.2",
"phonegap-plugin-push": {
"ANDROID_SUPPORT_V13_VERSION": "27.+",
"FCM_VERSION": "11.6.2"
}
*set to config.xml
<allow-navigation href="http://*/*" />
<allow-navigation href="https://*/*" />
<allow-navigation href="data:*" />
<allow-navigation href="tel:*" />
=============
===== ANDROID (phonegap-plugin-push > 2.0) =====
insert google-services.json to platforms/android/app/
*generator
<platform name="android">
<resource-file src="google-services.json" target="app/google-services.json" />
*doc
https://developers.google.com/android/guides/google-services-plugin
* phonegap-plugin-push < 2.0
<plugin...
<variable name="SENDER_ID" value="{firebase settings>Cloud Messaging> SenderID}"/>
--- handle errors ---
"Execution failed for task ':app:processDebugGoogleServices'.
> Please fix the version conflict either by updating the version of the google-services plugin (information about the latest version is available at https://bintray.com/android/android-tools/com.google.gms.google-services/) or updating the version of com.google.android.gms to 11.6.2."
- You must use only one version for all 3 libs in build.gradle
(for example "11.8.0" instead of "11.6.2"):
compile 'com.google.firebase:firebase-messaging:11.8.0'
compile 'com.google.android.gms:play-services-maps:11.8.0'
compile 'com.google.android.gms:play-services-location:11.8.0'
===========
===== IOS (creating certificates & provisioning profile)=====
1. on https://developer.apple.com/account/ios/identifier/bundle
- App IDs -> "appName" edit -> Push Notifications -> create production! SSL certificate
- Create a CSR file following instructions (create *.certSigningRequest)
- upload CSR (*.certSigningRequest)
- download SSL *.cer -> add to keychain on mac(2click)
2. on https://developer.apple.com/account/ios/certificate/distribution
- download iOS Distribution certificate *.cer
- add to keychain on mac(2click)
3. on https://developer.apple.com/account/ios/profile/
- Distribution Ad Hoc following instructions (Select certificate to include from step.2)
- download *.mobileprovision -> add to Xcode(2click)
4. in Xcode
- General -> Signing -> uncheck "Automatically manage signing"
- select created Provisioning profile(*.mobileprovision) for Signing(Debug)/(Release)
- Build Settings -> Code Signing Identify:
Debug/Release: iOS Developer
Any iOS SDK: certificate from step.2(identities in keychain)
5. in keychain (for tests)
- create *.p12 via exporting certificate(step.2) from keychain
--- handle errors ---
"ld: library not found for -lGoogleToolboxForMac"
first way:
clear Xcode product
$ cordova platform rm ios
$ cordova platform add ios
$ ionic cordova build ios
open xcproject then xcworkspace
second way:
go to platforms/ios
$ sudo gem install cocoapods
$ pod setup
============
===== IMPLEMENTATION =====
//app.module.ts:
import { Push } from '@ionic-native/push';
@NgModule({
...
providers: [..., Push]
...
})
// component.ts
import { Push, PushObject, PushOptions } from "@ionic-native/push";
constructor(
private push: Push,
//private authService: AuthorizeService,
) {
platform.ready().then(() => {
setTimeout(() => {
this.pushInit();
}, 2000);
});
}
presentToast(text) {
let toast = this.toastCtrl.create({
message: text,
duration: 2500,
position: 'top'
});
toast.present();
}
pushInit() {
this.push.hasPermission()
.then((res: any) => {
if (res.isEnabled) {
console.log('We have permission to send push notifications');
} else {
console.log('We do not have permission to send push notifications');
}
});
const options: PushOptions = {
android: {
//senderID: '87589863427',
sound: true,
vibrate: true,
icon: "ic_push",
iconColor: "#ffffff",
},
ios: {
alert: true,
badge: true,
sound: true,
clearBadge: true
},
windows: {},
browser: {
pushServiceURL: 'http://push.api.phonegap.com/v1/push'
}
};
const pushObject: PushObject = this.push.init(options);
pushObject.on('notification').subscribe((notification: any) => {
if(notification.additionalData.foreground) {
this.presentToast(notification.message);
}
});
pushObject.on('registration').subscribe((registration: any) => {
console.log('Device registered', registration);
this.pushTokenUpdate({
type: this.platform.is('ios') ? 'ios' : 'android',
pushToken: registration.registrationId
});
});
pushObject.on('error').subscribe(error => console.error('Error with Push plugin', error));
}
==============
====== TESTS ======
--- ANDROID ---
1. manually cloud Messaging in firebase
2. FCM (postman)
- method: "post"
- url: https://fcm.googleapis.com/fcm/send
- headers:
Content-Type , application/json
Authorization , key= firebase settings ->Cloud Messaging -> Legacy server key
- body:
{
"to": 'registrationId' from response pushObject.on('registration'),
"notification":{
"body":"test",
"title":"test",
"sound": "true"//default
},
"data":{}
}
--- IOS ---
Download https://github.com/noodlewerk/NWPusher
- import *.p12 from iOS step.5
- uncheck "Should use sandbox enviroment"
- insert device push token
- insert template:
{"aps":{"alert":"Testing.. (0)","sound":"default"}}
- init
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment