0

Below is my code for index.html in www folder , Sender id has been changed as needed from console.firebase.google.com . But Still not receiving any notification when i tried connecting and running app through phonegap desktop app . Sorry but this is first time i'm integrating push notification in app . So I need your help please . Thanks in advance

<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="msapplication-tap-highlight" content="no" />
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width" />
<!-- This is a wide open CSP declaration. To lock this down for production, see below. -->
<meta http-equiv="Content-Security-Policy" content="default-src * 'unsafe-inline'; style-src 'self' 'unsafe-inline'; media-src *" />
<link rel="stylesheet" type="text/css" href="css/index.css" />
<title>Hello World</title>
</head>

<body>
<div class="app">
<h1>PhoneGap</h1>
<div id="registration" class="blink">
<p class="event waiting">Requesting Registration ID</p>
<p class="event received">Registered</p>
</div>
</div>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
app.initialize();
</script>
</body>

</html>

below is my code in javascript (index.js)

var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
console.log('Received Device Ready Event');
console.log('calling setup push');
app.setupPush();
},
setupPush: function() {
console.log('calling push init');
var push = PushNotification.init({
"android": {
"senderID": "XXXXXXX"
},
"browser": {},
"ios": {
"sound": true,
"vibration": true,
"badge": true
},
"windows": {}
});
console.log('after init');

push.on('registration', function(data) {
console.log('registration event: ' + data.registrationId);
var oldRegId = localStorage.getItem('registrationId');
if (oldRegId !== data.registrationId) {
// Save new registration ID
localStorage.setItem('registrationId', data.registrationId);
// Post registrationId to your app server as the value has changed
}

var parentElement = document.getElementById('registration');
var listeningElement = parentElement.querySelector('.waiting');
var receivedElement = parentElement.querySelector('.received');

listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
});

push.on('error', function(e) {
console.log("push error = " + e.message);
});

push.on('notification', function(data) {
console.log('notification event');
navigator.notification.alert(
data.message,         // message
null,                 // callback
data.title,           // title
'Ok'                  // buttonName
);
});
}
};

below is my config.xml

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.phonegap.push" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0">
<name>phonegappush</name>
<description>
Push sample application that receives push notifications.
</description>
<author email="support@phonegap.com" href="http://phonegap.com">
PhoneGap Team
</author>
<content src="index.html" />
<preference name="DisallowOverscroll" value="true" />
<preference name="android-minSdkVersion" value="14" />
<preference name="StatusBarOverlaysWebView" value="false" />
<preference name="StatusBarBackgroundColor" value="#ee6e73" />
<preference name="StatusBarStyle" value="blacktranslucent" />
<plugin name="cordova-plugin-statusbar" source="npm" spec="~2.1.3" />
<plugin name="phonegap-plugin-push" source="npm" spec="~1.8.0">
<variable name="SENDER_ID" value="XXXXXXX" />
</plugin>
<plugin name="cordova-plugin-console" source="npm" spec="~1.0.3" />
<plugin name="cordova-plugin-dialogs" source="npm" spec="~1.2.1" />
<plugin name="cordova-plugin-whitelist" source="npm" spec="~1.2.1" />
<platform name="android">
<icon density="ldpi" src="www/res/icon/android/drawable-ldpi-icon.png" />
<icon density="mdpi" src="www/res/icon/android/drawable-mdpi-icon.png" />
<icon density="hdpi" src="www/res/icon/android/drawable-hdpi-icon.png" />
<icon density="xhdpi" src="www/res/icon/android/drawable-xhdpi-icon.png" />
<icon density="xxhdpi" src="www/res/icon/android/drawable-xxhdpi-icon.png" />
<icon density="xxxhdpi" src="www/res/icon/android/drawable-xxxhdpi-icon.png" />
<splash density="land-ldpi" src="www/res/screen/android/drawable-land-ldpi-screen.png" />
<splash density="land-mdpi" src="www/res/screen/android/drawable-land-mdpi-screen.png" />
<splash density="land-hdpi" src="www/res/screen/android/drawable-land-hdpi-screen.png" />
<splash density="land-xhdpi" src="www/res/screen/android/drawable-land-xhdpi-screen.png" />
<splash density="land-xxhdpi" src="www/res/screen/android/drawable-land-xxhdpi-screen.png" />
<splash density="land-xxxhdpi" src="www/res/screen/android/drawable-land-xxxhdpi-screen.png" />
<splash density="port-ldpi" src="www/res/screen/android/drawable-port-ldpi-screen.png" />
<splash density="port-mdpi" src="www/res/screen/android/drawable-port-mdpi-screen.png" />
<splash density="port-hdpi" src="www/res/screen/android/drawable-port-hdpi-screen.png" />
<splash density="port-xhdpi" src="www/res/screen/android/drawable-port-xhdpi-screen.png" />
<splash density="port-xxhdpi" src="www/res/screen/android/drawable-port-xxhdpi-screen.png" />
<splash density="port-xxxhdpi" src="www/res/screen/android/drawable-port-xxxhdpi-screen.png" />
<resource-file src="google-services.json" target="google-services.json" />
</platform>
<platform name="ios">
<icon height="57" platform="ios" src="www/res/icon/ios/icon.png" width="57" />
<icon height="114" platform="ios" src="www/res/icon/ios/icon@2x.png" width="114" />
<icon height="40" platform="ios" src="www/res/icon/ios/icon-40.png" width="40" />
<icon height="80" platform="ios" src="www/res/icon/ios/icon-40@2x.png" width="80" />
<icon height="50" platform="ios" src="www/res/icon/ios/icon-50.png" width="50" />
<icon height="100" platform="ios" src="www/res/icon/ios/icon-50@2x.png" width="100" />
<icon height="60" platform="ios" src="www/res/icon/ios/icon-60.png" width="60" />
<icon height="120" platform="ios" src="www/res/icon/ios/icon-60@2x.png" width="120" />
<icon height="180" platform="ios" src="www/res/icon/ios/icon-60@3x.png" width="180" />
<icon height="72" platform="ios" src="www/res/icon/ios/icon-72.png" width="72" />
<icon height="144" platform="ios" src="www/res/icon/ios/icon-72@2x.png" width="144" />
<icon height="76" platform="ios" src="www/res/icon/ios/icon-76.png" width="76" />
<icon height="152" platform="ios" src="www/res/icon/ios/icon-76@2x.png" width="152" />
<icon height="29" platform="ios" src="www/res/icon/ios/icon-small.png" width="29" />
<icon height="58" platform="ios" src="www/res/icon/ios/icon-small@2x.png" width="58" />
<icon height="87" platform="ios" src="www/res/icon/ios/icon-small@3x.png" width="87" />
<splash height="1136" platform="ios" src="www/res/screen/ios/Default-568h@2x~iphone.png" width="640" />
<splash height="1334" platform="ios" src="www/res/screen/ios/Default-667h.png" width="750" />
<splash height="2208" platform="ios" src="www/res/screen/ios/Default-736h.png" width="1242" />
<splash height="1242" platform="ios" src="www/res/screen/ios/Default-Landscape-736h.png" width="2208" />
<splash height="1536" platform="ios" src="www/res/screen/ios/Default-Landscape@2x~ipad.png" width="2048" />
<splash height="768" platform="ios" src="www/res/screen/ios/Default-Landscape~ipad.png" width="1024" />
<splash height="2048" platform="ios" src="www/res/screen/ios/Default-Portrait@2x~ipad.png" width="1536" />
<splash height="1024" platform="ios" src="www/res/screen/ios/Default-Portrait~ipad.png" width="768" />
<splash height="960" platform="ios" src="www/res/screen/ios/Default@2x~iphone.png" width="640" />
<splash height="480" platform="ios" src="www/res/screen/ios/Default~iphone.png" width="320" />
</platform>
<platform name="wp8">
<icon height="99" platform="wp8" src="www/res/icon/wp8/ApplicationIcon.png" width="99" />
<icon height="159" platform="wp8" src="www/res/icon/wp8/Background.png" width="159" />
<splash height="1280" platform="wp8" src="www/res/screen/wp8/screen-portrait.jpg" width="768" />
</platform>
<platform name="windows">
<icon height="150" platform="windows" src="www/res/icon/windows/Square150x150Logo.scale-100.png" width="150" />
<icon height="30" platform="windows" src="www/res/icon/windows/Square30x30Logo.scale-100.png" width="30" />
<icon height="50" platform="windows" src="www/res/icon/windows/StoreLogo.scale-100.png" width="50" />
<splash height="300" platform="windows" src="www/res/screen/windows/SplashScreen.scale-100.png" width="620" />
<icon height="120" platform="windows" src="www/res/icon/windows/StoreLogo.scale-240.png" width="120" />
<icon height="44" platform="windows" src="www/res/icon/windows/Square44x44Logo.scale-100.png" width="44" />
<icon height="106" platform="windows" src="www/res/icon/windows/Square44x44Logo.scale-240.png" width="106" />
<icon height="70" platform="windows" src="www/res/icon/windows/Square70x70Logo.scale-100.png" width="70" />
<icon height="71" platform="windows" src="www/res/icon/windows/Square71x71Logo.scale-100.png" width="71" />
<icon height="170" platform="windows" src="www/res/icon/windows/Square71x71Logo.scale-240.png" width="170" />
<icon height="360" platform="windows" src="www/res/icon/windows/Square150x150Logo.scale-240.png" width="360" />
<icon height="310" platform="windows" src="www/res/icon/windows/Square310x310Logo.scale-100.png" width="310" />
<icon height="150" platform="windows" src="www/res/icon/windows/Wide310x150Logo.scale-100.png" width="310" />
<icon height="360" platform="windows" src="www/res/icon/windows/Wide310x150Logo.scale-240.png" width="744" />
<splash height="1920" platform="windows" src="www/res/screen/windows/SplashScreenPhone.scale-240.png" width="1152" />
</platform>
<access origin="*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
<platform name="android">
<allow-intent href="market:*" />
</platform>
<platform name="ios">
<allow-intent href="itms:*" />
<allow-intent href="itms-apps:*" />
</platform>
</widget>
phpdev
  • 89
  • 2
  • 10
  • check the problem sequentially, what have you found in console in each stage? – Jobayer Aug 26 '17 at 06:13
  • i check with alert , it is alert everywhere . except push.on('notification', function(data) { – phpdev Aug 26 '17 at 06:19
  • @Jobayer though i'm not a expert in checking with console but what i can check , it is saying "Uncaught ReferenceError: PushNotification is not defined at Object.setupPush (index.js:24) at Channel.onDeviceReady (index.js:20) at Channel.fire (cordova.js:777) at cordova.js:231" – phpdev Aug 26 '17 at 06:22
  • it's not finding PushNotification plugin correctly, try to solve this issue using their official docs. – Jobayer Aug 26 '17 at 06:35
  • @Jobayer struggling to make it work from last three days , and trying everything have read the docs available checked in stackoverflow but unable to make it work . – phpdev Aug 26 '17 at 06:45
  • do you see that error in your desktop browser console? – David Hope Aug 26 '17 at 08:06

2 Answers2

0

I have done it for a phoneGap app, using this in config file for push notification plugin <plugin name="phonegap-plugin-push" spec="~1.10.5" source="npm" /> and this is my code for initialization

var push = PushNotification.init({
                "android": {"senderID": "your sender id", "sound": true, 
"vibrate": true},
                "ios": {"alert": true, "badge": true, "sound": true, 
"clearBadge": true}
            });

This is for notification

push.on('notification', function(data) {
                alert(JSON.stringify(data));
                setTimeout(function(){
                    $("#notification_button").trigger("click");
                }, 1000);
            });
Jobayer
  • 1,221
  • 1
  • 14
  • 22
  • still no success :( . – phpdev Aug 29 '17 at 07:16
  • never going into push.on('notification', function(data) { . – phpdev Aug 29 '17 at 08:05
  • push.on('registration', function(data) { is working , i'm able to alert till last in this function . – phpdev Aug 29 '17 at 08:07
  • Are your saving device data to database for sending notification? – Jobayer Sep 03 '17 at 13:04
  • i'm using cordova-plugin-fcm now and was able to receive notifications now , but FCMPlugin.getToken( never get called as i want token for the device , i tried placing on ready and after all the script ends but no success . – phpdev Sep 14 '17 at 06:11
  • I have used push.on('registration', function(data) {}) for saving device data to database, using a ajax call, I am sending data to server. – Jobayer Sep 24 '17 at 03:52
0

Unless I'm missing something, In your code, I don't see anywhere that you store the users device ID!

If you don't save their device ID, how was you expecting to send them a PN?!

I personally use PHP to send the PNs.

use this in your config.xml file:

<plugin name="phonegap-plugin-push" source="npm" spec="~1.4.2">
    <param name="SENDER_ID" value="xxxxxxx" />
</plugin>

You need to generate the sender ID in your google firebase console.

Put this in your html file:

var push = PushNotification.init({
    android: {
        senderID: "xxxxxxxxxxxx",
        sound: true,
        vibrate: true,
        alert: true
    },
    browser: {
        pushServiceURL: 'http://push.api.phonegap.com/v1/push'
    },
    ios: {
        alert: true,
        badge: true,
        sound: true
    },
    windows: {}
});

push.on('registration', function(data) {
console.log(data.registrationId);


var user = data.registrationId;


////Here you alert the users device ID... remove this alert in production//////

alert(user);

////Here we send the users ID to our server and will store in in our database so we can use the ID to send them the PN's/////////


var dataString="&user="+user+"&add=";

if($.trim(user).length>0)
{
$.ajax({
type: "POST",
url: 'https://yoursite.com/write.php',
data: dataString,
crossDomain: true,
cache: false,
beforeSend: function(){},
success: function(data){

//alert(data);

}
});
} 



});


////////Here we just show them the PN in alert(), use something more elegant to show them the PN.///////

push.on('notification', function(data) {

console.log(data.message);

data = data.message;

alert(data);



//alert(data.title+" Message: " +data.message);
// data.title,
// data.count,
// data.sound,
// data.image,
// data.additionalData
});

push.on('error', function(e) {
console.log(e.message);
});

Done.

Happy coding.

David Hope
  • 1,426
  • 4
  • 21
  • 50
  • Thanks for your help , but i'm still struggling changed sender id but no alerts this time :( . – phpdev Aug 27 '17 at 12:16
  • I'm sorry but i'm really having no clue what to do next , can you please please do ma a favor please . Can you please provide me three files index.html , index.js and config.xml ,write.php that i can just put and see if it is working , i'll be really thankful to you for this from bottom of my heart as it has been more then 10 days i'm struggling with this , rest all is working fine in app . Please need your kind help . – phpdev Aug 27 '17 at 12:43
  • never going into , push.on('notification', function(data) { , the user register id is also in localStorage.setItem('registrationId', data.registrationId); already as you said it is not in code . Please guide . – phpdev Aug 29 '17 at 07:47
  • start by debugging your code and look in the debug console. – David Hope Aug 29 '17 at 09:49