Help me out devs, I am trying to make a device to device push notification app using ionic 3 and one signal. The device is alrady connected to one signal
Im using ionic 3.19.0 cordova 8.0.0
Here are the codes of the two pages
This is the home page
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import {OneSignal} from '@ionic-native/onesignal';
import {Platform} from 'ionic-angular';
import { HttpModule } from '@angular/http';
import firebase from 'firebase';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
constructor(public navCtrl: NavController, public platform: Platform,
public One: OneSignal) {
}
ionViewDidLoad(){
this.Update_User_Presence()
}
//Update The Users Phone Id On Firebase For The First Time.
Update_User_Presence(){
//Make refrence to the users database, under the node UserProfile
let _userprofile = firebase.database().ref(`UserProfile/`);
//check if we are running on a device.
if (this.platform.is('cordova')) {
//Get the Id
this.One.getIds().then( success => {
//Update the database with onesignal_ID
_userprofile.update({
onesignal_ID: success.userId
})
})
}else{
_userprofile.update({
onesignal_ID: '12345567890'
})
}
}
//send the notification
Send_Push_Notification_To_Available_Users(){
//Make refrence to the users database, under the node UserProfile
let _userprofile = firebase.database().ref(`UserProfile/`);
let reciever_ID;
//Get the Ids available on the database
_userprofile.on('value', Snapshot => {
//snapshot data from the UserProfile node
let key = Snapshot.key
reciever_ID = Snapshot.val().onesignal_ID
})
///Push The Notification
if (this.platform.is('cordova')) {
let notificationObj:any = {
include_player_ids: reciever_ID,
contents: {en: "Hello, This Is My First Notification With Onesignal"},
};
this.One.postNotification(notificationObj).then( success => {
console.log("Notification Post Success:", success);
}, error => {
console.log(error);
alert("Notification Post Failed:\n" + JSON.stringify(error));
});
}
}
}
This is tha app.component.ts
import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import {OneSignal} from '@ionic-native/onesignal';
import { HomePage } from '../pages/home/home';
@Component({
templateUrl: 'app.html'
})
export class MyApp {
rootPage:any = HomePage;
constructor(public platform: Platform, public statusBar: StatusBar, public
One: OneSignal, public splashScreen: SplashScreen) {
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
statusBar.styleDefault();
splashScreen.hide();
});
}
initializeApp(){
this.platform.ready().then(() => {
let appId = 'aceaf149-6405-4a35-b438-069272189b85'
let googleProjectNumber = '967280993155'
if (this.platform.is('cordova')) {
this.One.startInit(appId, googleProjectNumber );
this.One.inFocusDisplaying(this.One.OSInFocusDisplayOption.Notification);
this.One.setSubscription(true);
this.One.enableVibrate(true);
this.One.handleNotificationReceived().subscribe((data) => {
// handle received here how you wish.
alert(data)
});
this.One.handleNotificationOpened().subscribe((data) => {
// handle opened here how you wish.
alert(data)
});
this.One.endInit();
this.statusBar.styleDefault();
this.statusBar.backgroundColorByHexString("#BBBBBB");
setTimeout(() => {
this.splashScreen.hide();
}, 500);
}
});
}
}
Please help me out here. Thank you
Aucun commentaire:
Enregistrer un commentaire