1

I am working on application in Nativescript which implements push notification. Lets say server sends push notification and based on action mentioned in payload of notification i will have to redirect in application. This redirection should be performed if user taps on notification from drawer and application is in background. Other case when application should not redirect if its in foreground. I have managed a flag for that as follow

app.js

application.on(application.launchEvent, function (args) {
   appSettings.setBoolean('AppForground', true);
});

application.on(application.suspendEvent, function (args) {
   appSettings.setBoolean('AppForground', false);
});

application.on(application.resumeEvent, function (args) {
   appSettings.setBoolean('AppForground', true);
});

application.on(application.exitEvent, function (args) {
   appSettings.setBoolean('AppForground', false);
});

application.on(application.lowMemoryEvent, function (args) {
   appSettings.setBoolean('AppForground', false);
});

application.on(application.uncaughtErrorEvent, function (args) {
   appSettings.setBoolean('AppForground', false);
});

And on Push notification listener

 var settings = {
    // Android settings
    senderID: '1234567890', // Android: Required setting with the sender/project number
    notificationCallbackAndroid: function(data, pushNotificationObject) { // Android: Callback to invoke when a new push is received.
        var payload = JSON.parse(JSON.parse(pushNotificationObject).data);
        if (appSettings.getBoolean('AppForground') == false){
            switch (payload.action) {

                case "APPOINTMENT_DETAIL":
                    frame.topmost().navigate({
                        moduleName: views.appointmentDetails,
                        context: {
                            id: payload.id
                        }
                    });  
                    break;

                case "MESSAGE":
                    frame.topmost().navigate({
                        moduleName: views.appointmentDetails,
                        context: {
                            id: payload.id,
                            from: "messages"
                        }
                    });
                    break;

                case "REFERENCES":
                    frame.topmost().navigate({
                        moduleName: views.clientDetails,
                        context: {
                            id: payload.id,
                            name: ""
                        }
                    });
                    break;

                default: 
            }
        }
    },

    // iOS settings
    badge: true, // Enable setting badge through Push Notification
    sound: true, // Enable playing a sound
    alert: true, // Enable creating a alert

    // Callback to invoke, when a push is received on iOS
    notificationCallbackIOS: function(message) {
        alert(JSON.stringify(message));
    }
};
pushPlugin.register(settings,
    // Success callback
    function(token) {
        // if we're on android device we have the onMessageReceived function to subscribe
        // for push notifications
        if(pushPlugin.onMessageReceived) {
            pushPlugin.onMessageReceived(settings.notificationCallbackAndroid);
        }
    },
    // Error Callback
    function(error) {
        alert(error);
    }
);

Now the problem, is that if application is in killed state and notification arrives. Then it sets flag to true as application is launched which it should not. So due to that redirection is not performed and in other cases when application is in foreground state then also its navigating through pages (which should not be) on receiving notification.

I doubt about flag management is causing the problem but not sure. Would you please guide me if anything is wrong with what i did ?

UPDATE

I am using push-plugin.

Thanks.

Hardik Vaghani
  • 2,163
  • 24
  • 46
  • I would check if data after arriving to device won't have background parameter then u would be able to determine if app was background or foreground – Marek Maszay Sep 16 '16 at 07:03
  • payload does not have background parameter, Its managed locally in `app.js`. based on that i have to manage navigation. – Hardik Vaghani Sep 16 '16 at 07:10

2 Answers2

1

I use this for notifications

https://github.com/EddyVerbruggen/nativescript-plugin-firebase

This plugin use FCM, it adds to datas received from notifications foreground parameter so from payload you can determine if app was background(foreground==false, app is not active or was started after notification arrived) or foreground(foreground==true, app is open and active), but you need to some changes to code as they are different plugins

Marek Maszay
  • 1,537
  • 1
  • 9
  • 11
  • I am working on In-Browser. How can i manage gradles and libraries ? – Hardik Vaghani Sep 16 '16 at 07:26
  • Even on the serverside i will have to make changes for sending push. Could you provide some demo/link ? – Hardik Vaghani Sep 16 '16 at 07:28
  • I don't have problem to send demo of server side code but not sure how to do it app side on Appbuilder if you are building whole app there or whats content of data from "function(data, pushNotificationObject)" from android callback? – Marek Maszay Sep 16 '16 at 07:36
  • { google.sent_time=1474027549237, smallIcon=small_icon, data={ "title": "New Appointment", "body": "John Smith is requesting an appointment on Friday, June 23 at 9:00PM (1-hour Incall)", "id": 2, "action": "APPOINTMENT_DETAIL" }, sound=1, title=NewAppointment, vibrate=1, google.message_id=0: 1474027549250154%d8dc7159f9fd7ecd, largeIcon=large_icon, message=JohnSmithisrequestinganappointmentonFriday, June23at9: 0 0PM(1-hourIncall), collapse_key=do_not_collapse } – Hardik Vaghani Sep 16 '16 at 12:06
  • Hi can you provide me steps you followed to get it running via CLI ? Its not complete in the plugin doc. – Hardik Vaghani Sep 21 '16 at 07:04
  • what u mean with running via CLI? – Marek Maszay Sep 21 '16 at 07:41
  • I am not using In-Broswer. I have created project with `tns create appName` – Hardik Vaghani Sep 21 '16 at 08:47
  • install plugins what u need + tns plugin add nativescript-plugin-firebase and go thought guide from firebase plugin in link – Marek Maszay Sep 21 '16 at 08:53
  • I did as steps mentioned in the doc but its giving error. – Hardik Vaghani Sep 21 '16 at 09:02
  • `A problem occurred evaluating root project 'nativescriptappbuilderspike'. > Failed to apply plugin [id 'com.google.gms.google-services'] > Version: 8.4.0 is lower than the minimum version (9.0.0) required for google-services plugin.` – Hardik Vaghani Sep 21 '16 at 10:15
  • So for this i found 8.4.0 in `include.gradle` and changed to 9.0.0 and then 9.4.0 – Hardik Vaghani Sep 21 '16 at 10:21
  • This error i got after resolving above error. `Execution failed for task ':buildMetadata'. > Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1` – Hardik Vaghani Sep 21 '16 at 10:45
  • Try change 9.4.0 to 9.0.2 for all versions in include.gradle, maybe it would be good to join NS community at slack too :) to avoid too many comments here – Marek Maszay Sep 21 '16 at 10:56
  • Thanks it was problem of 2 different versioned Google libraries. – Hardik Vaghani Sep 21 '16 at 12:01
  • 1 More problem. If app is in foreground then i am able to get all json payload and if app is in backgound then just `{"foreground":false,"google.sent_time":{},"google.message_id":"0:1474461696691075%d8dc7159d8dc7159"}` – Hardik Vaghani Sep 21 '16 at 12:43
  • Not sure if you are using notification or data payload :/ but for this plugin on android it takes notification payload data and you should get all datas for app from `onMessageReceivedCallback: function(message) {}` callback at least that worked that way – Marek Maszay Sep 21 '16 at 13:06
  • I want to send custom array so i will have to use `data` type not `notification`. SO that i can navigate based upon received data in payload but application is crashing if i send payload in `data` type of notification. – Hardik Vaghani Sep 22 '16 at 04:32
  • How are you handling redirection on notification tap ? – Hardik Vaghani Sep 22 '16 at 05:02
  • 1
    you can use `notification { custom_tag:"data"}` and with ui/frame `topmost().navigate({...})` – Marek Maszay Sep 22 '16 at 06:07
  • What i did is sending both `notification` and `data` and that resolved my problem. – Hardik Vaghani Sep 22 '16 at 06:12
  • Have you implemented FCM for ios ? i could use your help. – Hardik Vaghani Sep 23 '16 at 04:15
  • Not yet only planning to use that for ios too but if you create app at firebase for ios there will be guide for ios mobiles how to add firebase to them – Marek Maszay Sep 23 '16 at 05:53
0

You can use pusher-nativescript npm module.

import { Pusher } from 'pusher-nativescript';
/*Observation using the above.
- Project gets build successfully.
- on run -> ERROR TypeError: pusher_nativescript__WEBPACK_IMPORTED_MODULE_6__.Pusher is not a constructor
- Use: import * as Pusher from 'pusher-nativescript';
- Make sure to install nativescript-websocket with this package.
*/
var pusher = new Pusher('Your_app_key', { cluster: 'your_cluster_name' });
var channel = pusher.subscribe('my-channel');
channel.bind('my-event', function(data) {
alert(JSON.stringify(data));
});