I try to use phonegap-plugin-push 2.x. Registration event fires correctly in browser and in PhoneGap Developer app but nothing happens in application obtained from build.phonegap.com. Init is pastedet in deviceready part of code. I'll appreciate any suggestions.
<!--Initializing Push Notification-->
const push = PushNotification.init({
android: {
senderID: "12345....." // the same without this line
},
browser: {
pushServiceURL: 'http://push.api.phonegap.com/v1/push'
},
ios: {
alert: "true",
badge: true,
sound: 'false'
},
windows: {}
});
push.on('registration', function(data) {
obj.regid = data.registrationId;
alert(data.registrationId); // no alert in built app
});
Have you included google-services.json, as required by version 2+ of the push plugin?
Related
I have an Ionic project. I can receive notifications when the app is open. But when the app is closed, notifications do not come. After sending 2-3 notifications, the application gives a closed warning. How can I receive notifications when the app is closed?
Ionic info:
Ionic:
Ionic CLI : 6.20.1 (C:\Users\xxxxx\AppData\Roaming\npm\node_modules\#ionic\cli)
Ionic Framework : #ionic/angular 6.5.0
#angular-devkit/build-angular : 13.2.6
#angular-devkit/schematics : 13.2.6
#angular/cli : 13.2.6
#ionic/angular-toolkit : 6.1.0
Cordova:
Cordova CLI : 11.0.0
Cordova Platforms : android 10.1.2
Cordova Plugins : cordova-plugin-ionic-keyboard 2.2.0, cordova-plugin-ionic-webview 5.0.0, (and 9 other plugins)
Utility:
cordova-res : 0.15.4
native-run : 1.7.1
System:
NodeJS : v18.13.0 (C:\Program Files\nodejs\node.exe)
npm : 9.3.0
OS : Windows 10
cordova-plugin-fcm-with-dependecy-updated 7.8.0 "Cordova FCM Push
Plugin"
My Code:
constructor(
private platform: Platform,
private splashScreen: SplashScreen,
private statusBar: StatusBar,
public route: Router,
public xDevice: Device,
private network: Network,
private fcm:FCM,
) {
this.initializeApp();
this.setupFCM();
}
private async setupFCM() {
this.platform.ready().then(() => {
try {
this.fcm.getInitialPushPayload().then((payload) => {
if(payload?.wasTapped) {
console.log("Received FCM when app is closed -> ", JSON.stringify(payload));
// call your function to handle the data
//this._handlePushNotificationData(payload);
}
});
this.fcm.requestPushPermission();
this.fcm.hasPermission();
this.fcm.getToken().then(token => {
console.log("Token: " + token);
console.log(token)
}).catch(error => {
console.log(error)
})
this.fcm.onNotification().subscribe(data => {
if (data.wasTapped) {
console.log('Received in background');
} else {
console.log('Received in foreground');
}
});
this.fcm.onTokenRefresh().subscribe(token => {
console.log(token)
});
} catch (error) {
console.log(error)
}
});
}
It asks for permission for notification, but when the application is closed, the notification does not come. I can't find the problem either because the app is closed. How can I solve this problem?
The issue you are facing is likely caused by the way Firebase Cloud Messaging (FCM) handles push notifications when the app is closed. FCM uses a notification tray icon to display push notifications when the app is closed. However, the notification may not appear if the user has disabled the notification tray icon for your app.
To troubleshoot this issue, you can try the following:
Ensure that you have correctly set up FCM in your app and that the correct credentials are being used.
Check if the notification tray icon is enabled for your app in the device settings.
Make sure that the FCM server key and sender ID are correctly configured in the Firebase console.
Check if the device token is being correctly retrieved and sent to the FCM server.
5.Test the notifications on different devices and see if the issue is specific to a certain device or if it's happening on all devices.
If the issue is still not resolved, you can try using a different push notification service or library that is better suited for handling push notifications when the app is closed.
I am tring to do push notification on my cordova android app with Cordova plugin for Firebase Cloud Messaging plugin. I have created and registered app in Google firebase console and downloaded google-services.json file and kept it in my project root folder. Then I am trying the functions provided in the plugin documentation on device ready but did not get the device token, it is blank. My cordova android version is 9.1.0. Can someone tell me why the device token is not generated? Please help me. My js code is below:
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
cordova.plugins.firebase.messaging.onMessage(function (payload) {
console.log("New foreground FCM message: ", payload);
});
cordova.plugins.firebase.messaging.onBackgroundMessage(function (payload) {
console.log("New background FCM message: ", payload);
});
cordova.plugins.firebase.messaging.requestPermission({ forceShow: true }).then(function ()
{
console.log("You'll get foreground notifications when a push message arrives");
});
cordova.plugins.firebase.messaging.getToken().then(function (token) {
alert("Got device token: ", token);
});
/*cordova.plugins.firebase.messaging.onTokenRefresh(function () {
console.log("Device token updated");
});*/
cordova.plugins.firebase.messaging.createChannel({
id: "mypushid",
name: "Push channel",
importance: 3
});
}
React Native project with push notifications, I use this.
I follow this tutorial.
I only need local notifications. But if I run this project I get an error default firebaseapp is not initialized in this process.
I follow this answer and get a google service.json from firebase to make it start (notification don't show up unfortunately). But is this also possible without firebase?
requestPermissions: Platform.OS === 'ios', This line you have to add
import { Platform} from 'react-native';
PushNotification.configure({
onRegister: function (token) {
console.log("TOKEN:", token);
},
onNotification: function (notification) {
console.log("NOTIFICATION:", notification);
},
permissions: {
alert: true,
badge: true,
sound: true,
},
popInitialNotification: true,
requestPermissions: Platform.OS === 'ios',// This line you have to add //import Platform from react native
});
This package does work without firebase, after installing and setting up clear gradle cache.
On Windows:
gradlew cleanBuildCache
On Mac or UNIX:
./gradlew cleanBuildCache
Yes, it is possible to push local notification without firebase. I implemented it in one of my projects. You can take reference from this comment.
And also you don't have to call register methods from example which is available in react-native-push-notifications repo.
PS: As author did not marked any of the above answers as resolved. I hope this will work.
I have added Bluetooth plugin using CLI and I added below code to index.html. But this is not working. I am new to phonegap. Please help to include this plugin. I have used the tanelign-bluetooth plugin.
<script>
window.bluetooth.pair(
function() {
alert('Pairing Successful');
},
function(err) {
alert('There was an error Pairing to a device'+ JSON.stringify(err));
}, deviceaddress);
You need add this first on the device ready event:
document.addEventListener("deviceready", yourCallbackFunction, false);
function yourCallbackFunction(){
window.bluetooth = cordova.require("cordova/plugin/bluetooth");
}
And add this js in your page:
https://github.com/tanelih/phonegap-bluetooth-plugin/tree/master/www
Like this: <script src="bluetooth.js"></script>
Documentation: https://github.com/tanelih/phonegap-bluetooth-plugin
I want to send sms using ionic framework.I install this plugin:
cordova plugin add https://github.com/cordova-sms/cordova-sms-plugin.git
In javascript:
document.addEventListener("deviceready", function() {
var options = {
replaceLineBreaks: false, // true to replace \n by a new line, false by default
android: {
intent: '' // send SMS with the native android SMS messaging
//intent: '' // send SMS without open any other app
//intent: 'INTENT' // send SMS inside a default SMS app
}
};
$cordovaSms
.send('+919915768727', 'This is some dummy text', options)
.then(function() {
alert('Success');
// Success! SMS was sent
}, function(error) {
alert('Error');
// An error occurred
});
});
Message is not send.this will alert error.I dont know where I am wrong.
cordova version: 5.2.0
Ionic version: 1.6.4
you have place the function of $cordovaSMS in an $ionicPlatform function as
$ionicPlatform.ready(function(){
$cordovaSms
.send($scope.form.number, $scope.form.message, options)
.then(function(result) {
console.log(result);
}, function(error) {
console.log(error);
})
})
and I am uploading a demo project for you in git hub follow the steps in readme.md file of github
Having any queries replay back