zetavg / react-native-system-notification

Android system notifications for React Native. Supports push notifications with GCM integrated.
https://www.npmjs.com/package/react-native-system-notification
244 stars 102 forks source link

Background Notification Not Shown #50

Open edo1493 opened 8 years ago

edo1493 commented 8 years ago

Hi, I am trying to use https://github.com/oney/react-native-gcm-android with this other component to receive Notifications on Android. However when the app is on the background, I am not able to display anything. I am posting my logcat and code.

The notification is received, but when I try to create it, I don't see anything. I wasn't adding the "small icon", so I thought it was failing for this reason. However, even after creating one, I am not getting anything.

GcmAndroid.addEventListener('notification', function(notification){
             console.log('receive gcm notification', notification);
             console.log(GcmAndroid.isInForeground);
             //var info = JSON.parse(notification.data.info);
        if (!GcmAndroid.isInForeground) {
                    console.log("Hello!");
          Notification.create({
            subject: "hello",
            message: "hello",
       smallIcon: "ic_stat_icon"
          });
        }
         });
06-09 13:25:38.810 28402 28402 D com.oney.gcm.GcmModule: GCMReceiveNotification BroadcastReceiver
06-09 13:25:38.813 28402 28710 I ReactNativeJS: 'receive gcm notification', { data: { default: 'ee', collapse_key: 'do_not_collapse' } }
06-09 13:25:38.835 28402 28711 I ReactSystemNotification: Notification Show: 87295
06-09 13:25:38.835 28402 28711 I ReactSystemNotification: Notification Created: 87295
06-09 13:25:38.842   650   650 E NotificationService: Not posting notification without small icon: Notification(pri=1 contentView=null vibrate=default sound=default tick defaults=0x7 flags=0x11 color=0x00000000 vis=PRIVATE)
06-09 13:25:38.873   650   650 E NotificationService: WARNING: In a future release this will crash the app: com.canddi
06-09 13:25:52.535   650  4185 D NetlinkSocketObserver: NeighborEvent{elapsedMs=2063216972, 192.168.1.1, [5203552F9596], RTM_NEWNEIGH, NUD_REACHABLE}
06-09 13:26:00.005   650  4185 D NetlinkSocketObserver: NeighborEvent{elapsedMs=2063224442, 192.168.1.1, [5203552F9596], RTM_NEWNEIGH, NUD_STALE}
pranaygp commented 8 years ago

GcmAndroid.addEventListener('notification', ... gets triggered when your app is backgrounded, but doesn't have a valid notification object Instead, you should be using

if (GcmAndroid.launchNotification) {
  var notification = GcmAndroid.launchNotification;

  console.log("Got notification while app asleep", notification);

  Notification.create({
    subject: notification.title,
    message: notification.message,
  });

  GcmAndroid.stopService();
} 
edo1493 commented 8 years ago

@pranaygp I can see the console.log("Hello") and that is the code they show for the GCMAndroid component. Further, if my adb logcat shows "ReactNativeJS: 'receive gcm notification'" and console logs its content, it means it gets triggered.

pranaygp commented 8 years ago

@edo1493 I just went over your issue again. I realize that I missed some stuff the first time I read it.

So, if your app is backgrounded (just hitting the home button doesn't do it, you need to close it completely) then the notification event doesn't get triggered. Instead, you need to use the GcmAndroid.launchNotification object globally in your index file (I provided an example of using it above).

If your app is in the foreground (open, or you only hit the home button (which only suspends the app)), you can use the function that you wrote above.

Now , in case your app is in the foreground, you want to remove this if statement if (!GcmAndroid.isInForeground) { so that the notification actually get's sent out (That's how I got mine to work)

edo1493 commented 8 years ago

@pranaygp I don't think I explained myself very clearly.

If the app is in foreground, the notification event is able to catch it and a system sound is created with the create method. This is okay.

If I hit the home button (app is in background), the notifications event is able to catch the incoming notification, but the create method doesn't generate anything.

I am trying to solve the second issue.

pranaygp commented 8 years ago

@edo1493 Try removing the check for GcmAndroid.isInForeground. That did the trick for me.

Also I should mention this: After your last comment, I checked my app again and upgraded it to use the latest version of this library. I'm having a similar issue too. I'd try using an older version of this (a release from last week or older)

edo1493 commented 8 years ago

@pranaygp As I have already mentioned, the problem is with the .create method. Everything goes through. I am not able to create the notification.

pranaygp commented 8 years ago

@edo1493 yeah, in that case, try reverting to a slightly older version. I believe the current build has an issue. I'm having the same thing.

edo1493 commented 8 years ago

If the app is completely closed, I get:

"undefined is not an object (evaluating RCTToastAndroid.SHORT)"

This is an issue I have already seen in GCMAndroid.

pranaygp commented 8 years ago

That sounds Ike an issue with RCTToastAndroid

pranaygp commented 8 years ago

I'd be happy to give your codebase a quick look if you want me to.

edo1493 commented 8 years ago

@pranaygp It's exactly as the example on https://github.com/oney/react-native-gcm-android

pranaygp commented 8 years ago

I don't see a reference to RCTToastAndroid on that example. That really could be what's causing your issue. I'd look into that.

pranaygp commented 8 years ago

Sorry for not being of much help. I'm still not able to recreate the issue you're having.

xphaul commented 8 years ago

@edo1493 did you managed to solve this issue? You are experiencing this because GcmAndroid.addEventListener('notification', is not fired when the app is on background (as what i am experiencing too) . And now im stuck . Did you find a work around bout this issue? thanks

rimzici commented 7 years ago

Hi team, background issue. Notification.addListener('press', function(e) { console.log(e); }); getting called when app running in foreground. But not when app is running in background. Someone help please. thanks in advance.

pranaygp commented 7 years ago

@Neson Do you have any thoughts on the discussion above?

edo1493 commented 7 years ago

@xphaul Sorry, I have seen this just now. No, I didn't solve it in the end and the project stopped there.