how to handle iOS notification callback on phonegap cordova? - ios

I don't know if the title say it properly but..
I have a cordova 1.9 app with push notifications using the PushNotification Plugin and UrbanAirship. Everything works fine.
Now I'd like to open a particular page of my app when I lauch/resume my app from a notification.
Is that possible using Javascript ?
I'm totally lost when reading objective-c.

Notifications causes problem to understand JSON structure.
It is not:
if(notifications.length > 0){
But:
if(notifications.notifications.length > 0){
Array is on: notifications.notifications[] structure.

I am not sure how you are trying to load the page but the simplest way would be to call for the pending notifications as it is in javascript and then use window.location.href to load the require page.
I am using this procedure to perform certain task when I have pending notification at application start:
function registerAirship() {
console.log("ready to register for airship");
window.plugins.pushNotification.registerDevice({alert:true, badge:true, sound:true},function(status) {
if (status.deviceToken) {
window.token = status.deviceToken;
if (status) {
registerUAPush(token, "https://go.urbanairship.com/", key, key1, function(){
window.plugins.pushNotification.getPendingNotifications(function(notifications) {
if(notifications.length > 0){
var note = notifications[0];
if(note.applicationLaunchNotification == "1"){
// use the note.aps and redirect to required page
}
}
});
});
} else {
alert("Registration Error: " + status);
}
}
});
}

I was in your position a few days ago. I decided it was easiest to hack objective-c.
So.
1) I found the function that handled my push notification (AppDelegate.m) and called a js function after objective-c had launched execution for handling the notification. After that line, I did this:
[viewController.webView stringByEvaluatingJavaScriptFromString:#"opensometab()"];
2) In the root of my web app application I added this to the pushnotification.js file (you should be able to put it anywhere but I wanted it here).
** My JS function was to activate a tab within the Viewport so yours may be different.
function opensometab(){
var tabPanel = Ext.Viewport.down('tabpanelname');
tabPanel.setActiveItem(3); //index number 3
}

Related

Trouble processing Airship deep links on Xamarin.iOS

I used to have deep linking working on Xamarin.iOS with this delegate:
[Register("MyApp.iOS.PushService")]
public class PushService : UAPushNotificationDelegate
{
public override void ReceivedNotificationResponse(UNNotificationResponse notificationResponse, Action completionHandler)
{
try
{
NSString key = new NSString("^d");
if (notificationResponse.Notification.ValueForKey(key) != null)
{
PushClient.HandleNotificationOpened(notificationResponse.Notification.ValueForKey(key).ToString());
}
}
catch (Exception ex)
{
}
completionHandler();
}
}
I was receiving deep link actions to Airship when someone tapped on a push notification... I would pass this into "PushClient" to handle the deep link. (It seemed like Airship used to pass this with field "^d" for some reason, so I just used that and it worked for a long time)
Recently this no longer works, so I'm wondering if this Delegate/Handler is no longer viable? How can I see the whole payload of the push notification now, and/or see the values I tried to pass over from a deep link?
I used to have a .apns file of JSON I'd drag onto an iOS Simulator to mock a deep link push notification but I've lost it. No matter what I put in the .apns file, this delegate function of mine doesn't see any custom keys, either.

Cordova PushPlugin onNotificationAPN(e) callback not working or defined

I am working with Cordova / PhoneGap plugin PushPlugin and I have it setup pretty well and working, including a test .php and .pem file on my local server using a live device (iPhone 5). IOS 8. There are a couple of issues, with the main one being the call to this function in index.html:
function onNotificationAPN(event) {
console.log(event);
if (event.alert) {
$("#app-status-ul").append('<li>push-notification: ' + event.alert + '</li>');
// showing an alert also requires the org.apache.cordova.dialogs plugin
navigator.notification.alert(event.alert);
}
if (event.sound) {
// playing a sound also requires the org.apache.cordova.media plugin
var snd = new Media(event.sound);
snd.play();
}
if (event.badge) {
pushNotification.setApplicationIconBadgeNumber(successHandler, event.badge);
}
}
The console output for event is:
{"event":"message","payload":{"aps":{"alert":"My first push notification!","sound":"default"}},"foreground":true}
When the App is in the foreground the function onNotificationAPN(event) doesn't fire. "event" appears to be returned in or is converted to JSON format.
The IOS registration line is:
pushNotification.register(tokenHandler, errorHandler, {"badge":"true","sound":"true","alert":"true","ecb":"onNotificationAPN"});
How can I modify the code to decode the JSON format and display the alert, play sound, set the badge when the application is in the foreground. There must also be some documentation about the formats that the callback can take and the various options. Otherwise, seems to be working.
After a little playing around I guess that the callback "event" is actually already a JSON object:
console.log(e.event);
console.log(e.payload.aps.alert);
console.log(e.payload.aps.sound);
console.log(e.foreground);
prints out message, My first push notification!, default and true in the log.

PushPlugin Not Asking for Permissions on iOS

I'm working on setting up Push Notifications for an iOS app using the Ionic Framework but I'm running into a problem.
I added the plugin using the following
ionic plugin add https://github.com/phonegap-build/PushPlugin.git
and then added the following to my services.js file:
window.onNotificationAPN = function(event){
alert(event);
}
angular.module('starter.services', [])
.run(function($ionicPlatform){
$ionicPlatform.ready(function($scope, $window){
var tokenHandler = function(result){
alert('tokenHandler' + result);
}
var errorHandler = function(error){
alert('errohandler' + error);
}
var pushNot = window.plugins.pushNotification;
pushNot.register(
tokenHandler,
errorHandler,
{
"badge":"true",
"sound":"true",
"alert":"true",
"ecb":"onNotificationAPN"
}
);
});
})
When I run the app on my test device (running iOS 6.1.6) using command+R or sending it using testflight the application runs but never asks for permission to send push notifications and the the tokenHandler function is never called. Any help would be greatly appreciated.
Thanks for asking the question. I was searching for this for hours.
Turns out the solution for me was making sure that the opening angular module line in the app.js file included the ionic.service.core module dependency.
Then everything worked.
NOTE: (i wish it had thrown an error instead of allowing the push.register call to actually return a token)
angular.module('starter', ['ionic', 'ionic.service.core'])
The way I was able to solve this was to switch over to using ngCordova's Push script with the PushPlugin.

monkey talk - how to judge there is one UI exists?

In QTP, we can use below code:
if obj1.exist(5)
obj1.click;
else
obj2.click
We can control the wait time and if one UI exists, we can do some operation.
how to work out the same situation in Monkey Talk with JS?
and can I get the orientation of the IOS device with JS?
Use the following to see if a component exists, Label * Verify %timeout=10000. This will wait 10 seconds to find a label. You can use JavaScript like the following...
try {
app.label().verify(); //if label exists
} catch(Exception err) {
app.debug().print("Label not found");
}
To get the orientation you can do the following, Device * var orientation

UIAutomation: Any way to dismiss "Would Like To Use Your Current Location" alert?

my app is using location services, and for automated testing, I want to be able to dismiss the "APP Would Like To Use Your Current Location" popup. However, when I try to do this in Instruments with a UIAutomation script, I get this error:
Fail: Could not start script, target application is not frontmost.
This kind of makes sense, because the alert is produced by a different process. But still, what is Apple's plan for helping people automate their tests in this situation?
**Try**
UIATarget.onAlert = function onAlert(alert)
{
return true;
}
alertTitle = target.frontMostApp().alert().name();
if(alertTitle==="APP Would Like To Use Your Current Location")
{
target.frontMostApp().alert().buttons()["OK"].tap();
}
The solution appears to be to use AppleScript, run after a sufficient delay to allow for the alert to appear. You tell it to click on the alert button in the window of the Simulator.
Use this code before you trigger the appearance of the Location request dialog. Note the special quotation marks around the app name.
UIATarget.onAlert = function onAlert(alert)
{
if( alert.name()=="“local” Would Like to Use Your Current Location" )
{
alert.buttons()["OK"].tap();
}
else
{
UIALogger.logFail( "Location request dialog expected.");
}
return true;
}

Resources