OneSignal push notification handle in angularjs controller - javascript

i have problem with handling OneSignal pushNotification in cordova app. My purpose is open display page from any controllers, every page has specific controller. Whenever notification come will do same thing, open display page. My code is here :
index.html
function onDeviceReady() {
document.removeEventListener('deviceready', onDeviceReady, false);
var notificationOpenedCallback = function(jsonData) {
//do some thing
};
window.plugins.OneSignal
.startInit("xxxxxxxxxxxxxxxxxxx", "xxxxxxxxxxxxx")
.handleNotificationOpened(notificationOpenedCallback)
.endInit();
}
controller
app.controller('startController', function ($scope, $http) {
var notificationOpenedCallback = function (jsonData) {
$scope.myNavigator.pushPage('form/displaypage.html');
};
window.plugins.OneSignal //get error : Uncaught TypeError: Cannot read property 'OneSignal' of undefined
.startInit("xxxxxxxxxxxxxxxxxxx", "xxxxxxxxxxxxx)
.handleNotificationOpened(notificationOpenedCallback)
.endInit();
});
How to put windos.plugins.OneSignal in controllers?

For cordova apps, you need to add the OneSignal plugin first. In the terminal, at the root of your cordova application, do
cordova plugin add onesignal-cordova-plugin
If you want to save this plugin to your config.xml, add the --save flag to the end of the command. This comes in handy if you're using shared version control.
This will install and register the plugin. You should then rebuild the project and try again on your device.
Further information about setup with the Cordova SDK can be found here

Related

Uncaught TypeError: Cannot read property 'OneSignal' of undefined", source: file:///android_asset/www/js/app

I'm trying to initialize OneSignal to a PhoneGap application.
I tried many ways. But always it gives following error,
Uncaught TypeError: Cannot read property 'OneSignal' of undefined",
source: file:///android_asset/www/js/app.js error.
This is my app.js file
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
oneSignal();
checkConnection();}
function oneSignal(){
console.log("It is calling");
var notificationOpenedCallback = function(jsonData) {
console.log('didReceiveRemoteNotificationCallBack: ' + JSON.stringify(jsonData));
};
window.plugins.OneSignal.init("my app id",
{googleProjectNumber: "my project number"},
notificationOpenedCallback);
// Show an alert box if a notification comes in when the user is in your app.
window.plugins.OneSignal.enableInAppAlertNotification(true);}
Somebody please help me to initialize OneSignal to my PhoneGap application.
The best way to initialize OneSignal is by adding the code in your controller.js file and make sure its in one of your controllers - it might be login, logout, anything.
in other words you need to add the code inside a set controller that responds to a certain action.
Cant explain it better but try that -..
Good luck
Just try installing your plugin, cross verify for it's availability in the plugins folder. Also confirm the corresponding update has been made in config.xml and fetch.json (in the plugins folder). The error usually arises in case the plugin is not installed properly.
Hope this helps... :)

Cordova In App Browser event not firing IOS

Trying to authenticate users on my ionic application through an external service and I need to use cordovas In app browser! The code works perfectly on android however on iOS the "loadstop" event never fires and thus, the browser never redirects itself back to the application. The code I have looks like this:
$rootScope.$on('$cordovaInAppBrowser:loadstop', function (e, event) {
console.log('inappbrowser loaded', event);
var regex = /* regex to determine if url is correct redirected url */
var res = regex.test(event.url);
alert('loaded: ' + event.url);
alert('regex result: ' + res);
if(res === true) {
$cordovaInAppBrowser.close();
}
});
if(okta) {
if (typeof window.localStorage.msRefreshToken === 'undefined') {
document.addEventListener('deviceready', function () {
$cordovaInAppBrowser.open('urlforExternalservicehere', '_blank', options);
}, false);
} else {
TokenStore.refreshAccessToken();
}
}
when the code is run no alert appears on the screen. Also, once the app has reached the external service and the username of the user is entered, it is then redirected to another url, which the user will then use another set of credentials to authenticate against. This in turn returns a token for the application to authenticate use.
Thus, in a perfect iOS world where it matches the current android experience, the loadstop event fires three times, and the third time the "loadstop" event would fire and the regex would return true and close the in app broswer.
If I need to supply more code to help solve this issue please let me know!
Cordova Version: 4.2.0
Ionic: 1.4.5
iOS: 8 and 9
Using NgCordova for Cordova functionality
UPDATE: when running the application on an emulator and checking the console logs, I find this error:
Error: Module cordova-plugin-inappbrowser.inappbrowser does not
exist., http://10.117.1.46:8100/cordova.js, Line: 1402
I have the plugin installed so I don't know how its missing the plugin. Does anyone have a remedy for this? Thanks!
iabRef = window.open('http://XYZ.php', '_blank', 'location=no,toolbar=no');
iabRef.addEventListener('loadstart', iabLoadStart);
iabRef.addEventListener('loadstop', iabLoadStop);
iabRef.removeEventListener('loaderror', iabLoadError);
iabRef.addEventListener('exit', iabClose);
iabRef.addEventListener('loadstart', function(event) {
if (event.url.match("mobile/close")) {
iabRef.close();
window.location = 'index.html';
}
}
);
Problem was my iOS platform wasn't latest.
So when you developing apps using cordova make sure your platform versions and plugins are up-to date with OS upgraded.
So all I had to do is
Removing iOS platform.
cordova platform rm ios
Adding iOS platform - Latest Version
cordova platform add ios
Removing the plugin cordova-plugin-inappbrowser
cordova plugin remove cordova-plugin-inappbrowser
Adding the plugin cordova-plugin-inappbrowser - Latest Version
cordova plugin add cordova-plugin-inappbrowser

Trigger local notification for iOS - Cordova/Phonegap

I am using this plugin and trying to trigger local notification for my Cordova iOS app with Local notifications plugin.
I dd following steps:
Installed plugin: cordova plugin add de.appplant.cordova.plugin.local-notification#0.7.7
Updated config.xml: gap:plugin name="de.appplant.cordova.plugin.local-notification"
Added the following JavaScript in head tag for
index.html
plugin.notification.local.promptForPermission(function (granted) {
alert("promptForPermission: "+granted);
});
plugin.notification.local.hasPermission(function (granted) {
alert("hasPermission: "+granted);
});
Have a button in index.html to create a local notification after 5 secs.
Code for that looks like this:
function setLocalNotification()
{
alert("from setLocalNotification");
var t = new Date();
t.setSeconds(t.getSeconds() + 3);
window.plugin.notification.local.add({
title: 'Scheduled with delay',
message: 'Test Message ',
date: t
});
alert("alert set");
};
I do see a prompt for user's permission and the alert from hasPermission method shows the value as true. But I am still not able to get the actual local notification I am trying to set by clicking a button. I have already updated the APPLocalNotification.m file for the this issue by copying this fix.
I don't see any more errors in the console log but I am still not able to trigger a local notification.
You should check if the device is ready ( document.addEventListener('deviceready', function () {
) and also check if the plugin is correctly added $ cordova plugin ls.
Make sure you have "< script type="text/javascript" src="cordova.js">
" in your html .
Good luck!

"gamecenter undefined" using a Game Center Cordova plugin

What am i doing wrong? I have set up Game Center in Xcode and iTunes connect, but yet in Javascript... I get an error saying gamecenter is not defined when I try to authenticate the user.
PLUGIN: https://github.com/leecrossley/cordova-plugin-game-center
var successCallback = function (user) {
alert(user.alias);
// user.alias, user.playerID, user.displayName
};
gamecenter.auth(successCallback, failureCallback);
My full JS code is here: https://github.com/GunZi200/Memory-Colour/blob/master/test.js
the first thing my program should do is authenticate the user[nothing happens], how come gamecenter isn't defined?
You have to wait for the device ready event before trying to use a plugin
document.addEventListener("deviceready", authUser, false);
function authUser(){
gamecenter.auth(successCallback, failureCallback);
}

Device.uuid plugins notification not displaying cordova iOS app

I have a cordova ios app. I use the UUID frequently in my app but now its not working it gets stuck at the alert that displays it.
onDeviceReady: function() {
alert("On device Ready"); // Hits this and thats it
alert(device.window.uuid); //does not display this and does not go on
app.receivedEvent('deviceready');
},
as well as when I do:
alert(device.window.uuid);
var pushNotification = window.plugins.pushNotification;
Try this:
alert(device.uuid);
http://docs.phonegap.com/en/3.3.0/cordova_device_device.md.html#device.uuid
If that doesn't work and you are building locally, make sure to run:
cordova plugin add org.apache.cordova.device

Categories

Resources