6

I have an ionic app made with ionic1 and using OneSignal for push notification. Currently, one signal works well for push notification when: - app is already open - app is in background or kill, clicking on the push notification

using these 2 functions handleNotificationReceived and handleNotificationOpened

window.plugins.OneSignal.handleNotificationReceived(function(jsonData) {
    $state.go('panicalarm');
    console.log("navigate to panicalarm page");
  })


window.plugins.OneSignal.handleNotificationOpened(function(jsonData) {      
    $state.go('panicalarm');
    console.log("navigate to panicalarm page");
  })

I am wondering, how do i handle the push notification when launching the app directly or from bringing the app from background to foreground without clicking on the push notification ? Is that possible ?

Axil
  • 3,606
  • 10
  • 62
  • 136
  • I am not familiar with either ionic or onesignal, but on Android you can clear all notifications like this: NotificationManager.cancelAll() I am not certain if this works for push notifications, but you could give it a try. – dazza5000 Feb 23 '18 at 03:53

2 Answers2

2

i would say we can implement this in the following ways :

Solution 1: you can Send push notification every time . Then, if the app is open, ignore/swallow the notification. It seems like a waste of resource for me

Solution 2: Keep track of the app's state in your own database and only send a push notification if the app isn't transition from the background to foreground. It Seems to me overly complex.

My Proposed Solution :Will keep the same code for the foreground and background but when the app will be transition from the Background to the foreground i would be showing the notification icon (Badge Icon) like below (A sample one)

enter image description here

  1. It attracts the User
  2. Never distracts
  3. will be disappeared after the User has seen the notification
Krsna Kishore
  • 8,233
  • 4
  • 32
  • 48
1

You can try with https://github.com/katzer/cordova-plugin-background-mode

it helps you to Keep app running in background

Manoj Bhardwaj
  • 736
  • 1
  • 9
  • 25
  • it has this issue - "Infinite background tasks are not official supported on most mobile operation systems and thus not compliant with public store vendors. A successful submssion isn't garanteed.". If app is in background, and open, is it clear there is no way to receive push information ? – Axil Feb 27 '18 at 01:25