I have a problem with push notifications unregister. I am able to register the device and get the token, also sending notifications too, but I would like to add the unregister feature too. Here is the code y wrote for it:
var unsubscribeNotification = function unsubscribeNotification() {
try {
pushNotification.unregister(
function(e) {
//unRegister Success!!!
alert('unRegister Success');
},
function(e) {
//unRegister Failed!!!
alert('unRegister Failed');
});
}
catch(err) {
//Handle errors here
alert(err.message);
}
}
I also put a button to run the unsubscribeNotification() function when you click it. After a few seconds the application stops, no error, no messages, nothing!
I am using a Galaxy S3, I think it has Android 4.1
Thanks for the help!
Related
I have an Ionic 4 app which I have integrated with Firebase for authentication etc. I want to implement Firebase's cloud messaging so I can push messages to my app on both Android and iOS. I have done this pretty easily on iOS and I have sent a message via Postman which shows on my iPhone and I see the JSON of the message I have sent. When I try it on Android it doesn't work. Both devices receive the message but handle it very diferently.
I have read in a lot of places that you need to set the click_action to FCM_PLUGIN_ACTIVITY but when I do that the app doesn't even open on Android. When I take it out the app loads when you click the message but it doesn't show the body of the message like on iOS in my alert.
import { FCM } from '#ionic-native/fcm/ngx';
...
constructor(public platform: Platform, public fcm: FCM)
...
this.platform.ready().then(() => {
this.fcm.onNotification().subscribe(data => {
alert(JSON.stringify(data));
});
this.fcm.onTokenRefresh().subscribe(token => {
// Register your new token in your back-end if you want
// backend.registerToken(token);
});
}).catch((error) => {
this.showFailureMessage(error.message);
});
This is what I am posting off to... https://fcm.googleapis.com/fcm/send
{
"notification":{
"title":"My Title",
"body":"My Body",
"sound":"default",
"click_action":"FCM_PLUGIN_ACTIVITY"
"icon":"fcm_push_icon",
},
"data":{
"type":"Something",
},
"to":"/topics/all",
"priority":"high",
"restricted_package_name":""
}
Any help would be very much appreciated.
You seem to be missing the data.wasTapped part in your subscribe. Here, try this:
this.fcm.onNotification().subscribe(data => {
if (data.wasTapped) {
alert('Received in background');
alert(JSON.stringify(data));
} else {
alert('Received in foreground');
alert(JSON.stringify(data));
}
});
Im building an app which sends questions to users, at a specific time.
Most of the times everything goes well, and the question appears at the right time on my screen.
But now I'm testing it with Firefox and on my Android device. I'm getting a push notification (on my phone) at the right time, but when I open the app, it still says I dont have any open questions. Same on firefox. When I refresh my internetbrowser, and when I restart the app on my phone, the questions have shown up.
I think this has something to do with the cache of the Meteor app? Does anyone know a proper solution to fix this issue? Or does this only happen when I run my app locally?
My publication looks like this:
Meteor.publish("questions", function () {
var currentUser = this.userId;
if (Roles.userIsInRole(currentUser, ['superadmin']) || Roles.userIsInRole(currentUser, ['admin'])){
return Questions.find({});
}
else if(!this.userId){
return null;
}
else{
var joinedProjects = Projects.find({
$or: [
{invited: { $in: [currentUser] }},
{accepted: { $in: [currentUser] }}
]
}).map(function (projects) { return projects._id; });
return Questions.find({ project: { $in: joinedProjects }});
}
});
I am developing an Web Application that uses GCM for push notifications. I am following this documentation. I am having problem with initializeState function in docs.
This is how I added service worker script: I just created a service-worker.js empty file and pointed to this file in code to register service. Totally empty file.
This is my JavaScript code
<script type="text/javascript">
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register("{{ url('js/service-worker.js') }}").then(initialiseState).catch(function(error){
alert('Unable to register service worker for notification')
});
} else {
alert('Your browser does not support service worker that is used to receive push notification');
}
function subscribe()
{
alert('Subscribed')
}
function unsubscribe()
{
alert('Unsubscribed')
}
// the problem is inside this function. This function is called after service worker registration
function initialiseState() {
if (!('showNotification' in ServiceWorkerRegistration.prototype)) {
alert("Notification is not supported in this browser")
return;
}
if (Notification.permission === 'denied') {
alert('Notifications are blocked')
return;
}
if (!('PushManager' in window)) {
alert('Push messaging is not supported in thiss browser');
return;
}
// Every code working until here. I already checked using else if to statement.
// We need the service worker registration to check for a subscription
navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) {
//The problem is here. It is not alerting anything
alert('Ready to check')
});
}
</script>
I commented in the code to highlight where the problem is. What I know is it should alert anything if it is working. So it is not alerting anything. So I cannot do other steps. I checked the console and it is not showing any error. Is it not working because my service-worker.js is empty? If not, how can I fix my code to work?
Given the following code:
Posts.update(currentPostId, {$set: postProperties}, function(error) {
if (error) {
// display the error to the user
alert(error.reason);
} else {
Router.go('postPage', {_id: currentPostId});
}
});
How does one take the following code and add showing a flash message telling the user that the item has been updated on top of being directed to the new message?
Have you looked at the various flash message packages?
naxio:flash in particular supports iron:router.
My application uses ACS Push Notification. I have implemented app badge in my application. But the problem is the appBadge doesn't incrementing automatically while receiving a push notification. I have used the following code in my app
var deviceToken;
Titanium.Network.registerForPushNotifications({
types: [
Titanium.Network.NOTIFICATION_TYPE_BADGE,
Titanium.Network.NOTIFICATION_TYPE_ALERT,
Titanium.Network.NOTIFICATION_TYPE_SOUND
],
success:function(e)
{
deviceToken = e.deviceToken;
SubscribeToPush(channelName, deviceToken, type);
},
error:function(e)
{
alert("Error: "+ ((e.error && e.message) || JSON.stringify(e.error)));
},
callback:function(e)
{
var badgeCount = Ti.UI.iPhone.getAppBadge();
badgeCount = badgeCount + 1;
Ti.UI.iPhone.setAppBadge(badgeCount);
}
});
I read here that "callback function" invoked upon receiving a new push notification. So I set the following code as callback to increment the badge.
callback:function(e)
{
var badgeCount = Ti.UI.iPhone.getAppBadge(); //Will return the app badges
badgeCount = badgeCount + 1; //Incrementing the appbadge
Ti.UI.iPhone.setAppBadge(badgeCount); //Setting new appbadge
}
It works while the app is open and when it receives a notification, callback get fired and when the app go to background, the badge get appeared. But I want to increment the badge number when the app is in background or exited. Can anyone help me to resolve this issue?
After lots of research I have created a sample application to increment the appBadge while receiving a server push notification. You can download the code from Increment the ios appBadge Titanium. Please follow the steps after downloading the resources folder.
Create a new mobile application project in Titanium.
Replace the resources folder with the one you downloaded.
Login to www.appcelerator.com, go to your app then go to Manage ACS
Create a new user as admin, set user as admin
Create a new Access Control List(ACS) using the admin user and give the ACL Name as 'SampleApp'
Upload the p12 certificate for push notification
Now Install the application to your iPhone and run the app...
Each user of the app should have a custom object which stores the number of notifications. I'm updating them while sending a push and clears it while I resume/open the application. I tested it with my iPhone devices and it works perfect. However it takes some delays since I have to call ACS multiple times.
UPDATE : Latest Titanium SDKs Support this feature by default.
What you need to do is to change the payload as follows:
var payload = {"alert":"Hi, This is a test notification", badge: "+1"};
/*+1 will increment the current appbadge by 1, number of appbadge will be saved in the ACS*/
Cloud.PushNotifications.notify({
channel: 'friend_request',
payload: payload
}, function (e) {
if (e.success) {
alert('Success');
} else {
alert('Error:\n' +
((e.error && e.message) || JSON.stringify(e)));
}
});
And this will increase the appbadge by one. And you need to reset the appbadge when you resume/open of your application as follows
Cloud.PushNotifications.resetBadge({
device_token : myDeviceToken
}, function(e){
if(e.success){
Ti.UI.iPhone.setAppBadge(0);
} else {
//Error callback
}
})