Ionic FCM push notifications observable does not emit - ionic-framework

I am doing the following in an ionic application:
When a user is logged in, subscribe to their user ID topic
When a push notification arrives, console.log something.
this.authState.subscribe((state) => {
if (state) {
this.fcmPush.subscribeToTopic(state.uid);
this.fcmPush.onNotification().subscribe(notification => {
if (notification.wasTapped) {
console.log('Received in background', notification);
} else {
console.log('Received in foreground', notification);
}
});
}
});
Source: https://github.com/AmitMY/ionic-starter-firebase/blob/master/src/app/app.component.ts#L118
Sending a notification to my own topic arrives (after a few minutes), but I never see anything in console, both from outside the app, and inside the app.
What am I doing wrong?

Sending a notification from firebase messaging does not work.
However, using the sdk I must add:
click_action: "FCM_PLUGIN_ACTIVITY",
Which is in the usage guide but I missed

Related

Which is better approach to send push notification in Kuzzle using hooks or subscription?

I’m using Kuzzle as backend for my realtime chat application.
What is the better approach to sending push notification when user is offline in a mobile chat app?
1. Using custom plugin hooks
// check for every message in chat
this.hooks = {
'document:afterCreate': (request) => {
if (request.input.resource.collection == 'messages') {
let message = request.input.body;
this.context.accessors.sdk.document.get('now', 'user_sessions', message.otherUserId).then((userSession) => {
if (!userSession._source.isOnline) {
userSession._source.devices.forEach(device => {
// send push notification
});
}
})
}
}
}
2. Subscription per user for all users after the Kuzzle server start-up
const app = new Backend('kuzzlebackend')
app.start()
.then(async () => {
// Application started
// Loops through all users and adds their subscriptions to Kuzzle
foreach(user in users) {
app.sdk.realtime.subscribe('now', 'messages', { ‘otherUserId' : user._id }, async (notification: Notification) => {
this.context.accessors.sdk.document.get('now', 'user_sessions', user._id).then((userSession) => {
if (!userSession._source.isOnline) {
userSession._source.devices.forEach(device => {
// send push notification
});
}
})
})
}
})
You should use the hook mechanism on the generic:document:afterWrite event to send notification to offline users when a new message is created.
This event will be triggered every time a document is written with one of the Document controller action, and for the m* action family it you will able to process documents in bulk instead of one by one.

How to close Camera using phonegap-barcode-scanner plugin within service IONIC

I am using phonegap-barcodescanner-plugin to read qr while a service is reading instant payments on background.
I am detecting a new instant payment in the service and launching an event subscribed in the page where the barcodeScanner is launched
event.subscribe('instant-payment', (val) => {
console.log("Hi--------------", val)
this.navCtrl.pop()
});
the event is correctly fired and the log is ok but I'm trying to do a navCtrl.pop() and the Activity is never closed, I thought was going to work same way like cancelling scanner
scan() {
this.barcodeScanner.scan(this.options).then(barcodeData => {
if (barcodeData.cancelled == true) {
this.navCtrl.pop()
} else {
}
}).catch(err => {
console.log('Error', err);
});
}
Is there anyway to force close barcodeScanner and go back to the last "IonicPage".
Thanks for any help.

fcm background ionic 3

i am using FCM in ionic 3
I am getting notification properly when the app is in foreground. When I receive a notification in background, on tapping of notification i am getting the data. But i want to get the data without tapping on the notification. For example see the below code.
this.fcm.onNotification().subscribe(data => {
if(data.wasTapped){
console.log("Received in background");
} else {
console.log("Received in foreground");
};
});
So, my problem is to get data in background without tapping the notification

On subscription run a "callback" function once

I'm working with cordova's BLE (bluetooth low energy)
After I subscribe to notifications of BLE (which returns Observable), I want to send some message to the ble device, what is the best way to perform this, basically I need to run a function once after the subscription is made so that once device responds back to me, the code in the subscription is run.
ble.startNotification(deviceId, uuid1, uuid2).subscribe(bufferData=> {
//do something with bufferData
})
now after this, I want to run something like a callback,
.then(()=> {
//send message to device (only once), after the message is sent, the device will respond back and the `do something with bufferData` code will be run
})
I could easily do a setTimeout and send a message to the device after few seconds, and of course it works, but I want to do it cleanly, after I'm sure the subscription happened (subscription to the Observable of course)
You can wrap existing method using create operator and add custom code that will be executed on every new subscription.
See the example:
// emulate cordova with "dummy" bluetooth interface
const BLE = {
startNotification: () => Rx.Observable.interval(1000)
}
const wrappedBLE = (...params) =>
Rx.Observable.create(observer => {
// constructor fn will be executed on every new subscribtion
const disposable = BLE.startNotification(...params).subscribe(observer);
// place code to send notification here, instead of console log
console.log('New subscriber for BLE with params: ', params);
return disposable;
});
wrappedBLE("param1", "param2", "param3")
.subscribe(e => console.log("received notification: ", e));
<script src="https://unpkg.com/rxjs#5.4.3/bundles/Rx.min.js"></script>

Ionic 1 push notification, push.register never been fire up

I've been followed the instruction on: http://docs.ionic.io/services/push/
And I use the code below in my after login function:
$ionicPush.register().then(function(t) {
console.log(t);
return $ionicPush.saveToken(t);
}).then(function(t) {
console.log('Token saved:', t.token);
});
but it the register function with console.log never been fire up after I login.
Any idea?
Also I'm not using ionic Auth, so i didn't save the device token into user obj, and I think the auth user is not mandatory for the ionicPush, is this correct?
You can totally use Ionic Push without Ionic Auth.
Are you trying to run this code in your browser or on a device? It will only work on a device.
I suggest you to catch errors on your register() call first:
$ionicPush.register().then(function(t) {
console.log(t);
}, function(error) {
console.log(error)
});
Thus, you will see why it's not working.