oney / react-native-gcm-android

GCM for React Native Android
MIT License
172 stars 76 forks source link

Notification does not disappear on status bar after it's clicked #12

Open ACCTFORGH opened 8 years ago

ACCTFORGH commented 8 years ago

The notification stays at the tool bar and sometimes freezes it. I am placing the addEventListener and requestPermissions() methods at index.android.js file and under componentWillMount() method. I am using RN0.15 and Android 4.2.2 on Genymotion. Is it because I use it wrong or similar cases are encountered by others as well? Thanks a lot!

oney commented 8 years ago

The configuration of notification is in here. https://github.com/oney/react-native-gcm-android/blob/master/android/src/main/java/com/oney/gcm/RNGcmListenerService.java#L90 Modify to .setAutoCancel(true) will do the trick. I recommend you create new GcmListenerService to handle notification in Java code by yourself. Like this commit https://github.com/oney/TestGcm/commit/5fa36144ed015f38e4608667702b27b36a9696c2

Kuzmin commented 8 years ago

@oney Rather than requiring users to write their own Java wrappers, which I think some people might be uncomfortable with (since they're on React Native), would it be possible to add methods for removing notifications from status bar and such behaviour inside the JS API?

That might lower barriers to entry and make this library usable for a wider audience.

oney commented 8 years ago

Of course it is possible. Because the bridge of JS and native will be paused by default when the app enters background(onPause), we can't handle notifications in JS code. Possible solution is In JS

GcmAndroid.Setting.setAutoCancel(true);

In java, store the setting using SharedPreferences

SharedPreferences preferences = context.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("GcmAndroid.Setting.autoCancel", autoCancel);
editor.commit();

And get the setting when getting notification.

SharedPreferences preferences = context.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
        .setAutoCancel(preferences.getBoolean("GcmAndroid.Setting.autoCancel", false))

May do it in the future and also welcome a PR!

ghost commented 8 years ago

Hello,

First of all, awesome plugin, thanks a lot for making it.

I got the notifications working, I can now send notifications and they pop up.

However, I think I'm having the same problem as above:

And I have another issue, which I'm not sure is related:

I'm having some trouble getting this to work. I have no Java experience so it's kinda hard. I tried implementing your commit: oney/TestGcm@5fa3614 and also created a listener and added the change in my AndroidManifest.xml. But it does not work for me.

To see if it changes anything at all, I tried setting ContentTitle/ContentText to something else like:

.setContentTitle("test")
.setContentText("some message")

Am I overlooking something? What am I doing wrong?

oney commented 8 years ago

@txix Which this module version you use?

ghost commented 8 years ago

@oney Thanks for your reply. I use the following node modules:

"react-native": "0.17.0",
"react-native-gcm-android": "^0.1.0",
"react-native-system-notification": "0.0.7"
oney commented 8 years ago

@txix You're right. Three issues exist. Please update to 0.1.1 from npm. These issue should be fixed. Make sure to re-read README and modify codes.

ghost commented 8 years ago

@oney First of: Thanks alot for the update!

However, I'm unable to get it to work.

I've copied the exact code from the README.md and put it in my index.android.js

I then changed all the things you mentioned in the README (looked at the commit).

The thing that works is registering, receiving a token and sending notifications. However I'm still unable to click them (I'm using the default RNGcmListenerService now, but I tried changing it and it had no effect).

I also see no console message from the 'notification' eventListener when I'm receiving the notification inside the app.

oney commented 8 years ago

It's ok. Can you try the demo again? https://github.com/oney/TestGcm You can use the curl command in README to test sending gcm notification.

oney commented 8 years ago

Run adb logcat and get logs when you click the notification, and paste the logs to me. Thanks!

ghost commented 8 years ago

Hey.

So I tried to run the TestGCM package.

The TestGCM package does not show me the notification though.

This is what I get when running adb logcat:

12-22 19:19:45.451 30699 30736 I GCM     : GCM message com.testgcm 0:1450808385799742%f719a9d2f9fd7ecd
12-22 19:19:45.495  9261  9319 D RNGcmListenerService: sendNotification
12-22 19:19:45.496  9261  9261 D com.oney.gcm.GcmModule: GCMReceiveNotification BroadcastReceiver
12-22 19:19:45.496  9261  9261 W unknown:React: Calling JS function after bridge has been destroyed.

I'll see what adb logcat says on my own app.

oney commented 8 years ago

Yeah, the source of the issue is unknown:React: Calling JS function after bridge has been destroyed.. Let me think about it.

ghost commented 8 years ago

Alright.

Both the TestGcm and my own app crash when they're closed and not running in the background.

My own app gives the same kinda log when it's open. When it's closed, this is the log:

12-22 19:30:14.503 11881 11897 D RNGcmListenerService: sendNotification
12-22 19:30:14.510 29959 29983 W BroadcastQueue: Unable to launch app com.testgcm/10118 for broadcast Intent { act=com.oney.gcm.GCMReceiveNotification flg=0x10 (has extras) }: process is bad
12-22 19:30:14.513 11881 11881 D com.oney.gcm.GcmModule: sadfjasidfjliasf444
12-22 19:30:14.528 11881 11881 D BackgroundService: onStartCommand
12-22 19:30:14.564 11881 11881 I art     : Thread[1,tid=11881,Native,Thread*=0xb4df6500,peer=0x73fd12a0,"main"] recursive attempt to load library "/data/app/com.myapp-2/lib/arm/libreactnativejni.so"
12-22 19:30:14.643 11881 11910 W unknown:React: You seem to be running on device. Run 'adb reverse tcp:8081 tcp:8081' to forward the debug server's port to the device.
12-22 19:30:14.704 11881 11914 E         : Unable to load script from assets: index.android.bundle
12-22 19:30:14.705 11881 11914 E ReactNative: Got JS Exception: ReferenceError: Can't find variable: __fbBatchedBridge
12-22 19:30:14.706 11881 11914 E AndroidRuntime: FATAL EXCEPTION: mqt_js

Thanks again for your time.

oney commented 8 years ago

Oh, crash when they're closed and not running in the background is due to lack of index.android.bundle. Run below command to generate index.android.bundle, and then run react-native run-android

react-native bundle --platform android --dev false --entry-file index.android.js \
  --bundle-output android/app/src/main/assets/index.android.bundle \
  --assets-dest android/app/src/main/res/
ghost commented 8 years ago

Alright! Got the following working now:

It still shows a dialog my app is crashed though. Even though the app opens after clicking on the notification.

12-23 06:03:57.132 30699 27756 I GCM     : GCM message com.myapp 0:1450847037856776%843f15aef9fd7ecd
12-23 06:03:57.203 29959 29983 I ActivityManager: Start proc 2415:com.myapp/u0a120 for broadcast com.myapp/com.google.android.gms.gcm.GcmReceiver
12-23 06:03:57.208  2415  2415 I art     : Late-enabling -Xcheck:jni
12-23 06:03:57.279  2415  2440 I GMPM    : App measurement is starting up
12-23 06:03:57.292  2415  2442 D RNGcmListenerService: sendNotification
12-23 06:03:57.293 29959 29983 W BroadcastQueue: Unable to launch app com.testgcm/10118 for broadcast Intent { act=com.oney.gcm.GCMReceiveNotification flg=0x10 (has extras) }: process is bad
12-23 06:03:57.295  2415  2415 D com.oney.gcm.GcmModule: sadfjasidfjliasf444
12-23 06:03:57.299  2415  2415 D BackgroundService: onStartCommand
12-23 06:03:57.340  2415  2415 I art     : Thread[1,tid=2415,Native,Thread*=0xb4df6500,peer=0x73fd12a0,"main"] recursive attempt to load library "/data/app/com.myapp-1/lib/arm/libreactnativejni.so"
12-23 06:03:57.400  2415  2445 W unknown:React: You seem to be running on device. Run 'adb reverse tcp:8081 tcp:8081' to forward the debug server's port to the device.
12-23 06:03:57.685  2415  2449 D com.oney.gcm.GcmModule: packageName: com.myapp
12-23 06:03:57.724  2415  2415 D AndroidRuntime: Shutting down VM
12-23 06:03:57.725 30242 30242 D PhoneStatusBar: disable: < expand ICONS* alerts SYSTEM_INFO* back home recent clock search quick_settings >
12-23 06:03:57.725  2415  2415 E AndroidRuntime: FATAL EXCEPTION: main
12-23 06:03:57.725  2415  2415 E AndroidRuntime: Process: com.myapp, PID: 2415
12-23 06:03:57.725  2415  2415 E AndroidRuntime: java.lang.AssertionError: Attached DialogModule to host with pending alert but no FragmentManager (not attached to an Activity).
12-23 06:03:57.725  2415  2415 E AndroidRuntime:    at com.facebook.infer.annotation.Assertions.assertNotNull(Assertions.java:28)
12-23 06:03:57.725  2415  2415 E AndroidRuntime:    at com.facebook.react.modules.dialog.DialogModule.onHostResume(DialogModule.java:199)
12-23 06:03:57.725  2415  2415 E AndroidRuntime:    at com.facebook.react.bridge.ReactContext.onResume(ReactContext.java:140)
12-23 06:03:57.725  2415  2415 E AndroidRuntime:    at com.facebook.react.ReactInstanceManagerImpl.moveReactContextToCurrentLifecycleState(ReactInstanceManagerImpl.java:702)
12-23 06:03:57.725  2415  2415 E AndroidRuntime:    at com.facebook.react.ReactInstanceManagerImpl.setupReactContext(ReactInstanceManagerImpl.java:545)
12-23 06:03:57.725  2415  2415 E AndroidRuntime:    at com.facebook.react.ReactInstanceManagerImpl.access$700(ReactInstanceManagerImpl.java:78)
12-23 06:03:57.725  2415  2415 E AndroidRuntime:    at com.facebook.react.ReactInstanceManagerImpl$ReactContextInitAsyncTask.onPostExecute(ReactInstanceManagerImpl.java:175)
12-23 06:03:57.725  2415  2415 E AndroidRuntime:    at com.facebook.react.ReactInstanceManagerImpl$ReactContextInitAsyncTask.onPostExecute(ReactInstanceManagerImpl.java:155)
12-23 06:03:57.725  2415  2415 E AndroidRuntime:    at android.os.AsyncTask.finish(AsyncTask.java:651)
12-23 06:03:57.725  2415  2415 E AndroidRuntime:    at android.os.AsyncTask.-wrap1(AsyncTask.java)
12-23 06:03:57.725  2415  2415 E AndroidRuntime:    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
12-23 06:03:57.725  2415  2415 E AndroidRuntime:    at android.os.Handler.dispatchMessage(Handler.java:102)
12-23 06:03:57.725  2415  2415 E AndroidRuntime:    at android.os.Looper.loop(Looper.java:148)
12-23 06:03:57.725  2415  2415 E AndroidRuntime:    at android.app.ActivityThread.main(ActivityThread.java:5417)
12-23 06:03:57.725  2415  2415 E AndroidRuntime:    at java.lang.reflect.Method.invoke(Native Method)
12-23 06:03:57.725  2415  2415 E AndroidRuntime:    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
12-23 06:03:57.725  2415  2415 E AndroidRuntime:    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

When the app is open, I see the following (only after I opened it and do nothing):

12-23 06:07:55.613 30699 27756 I GCM     : GCM message com.myapp 0:1450847276352989%843f15aef9fd7ecd
12-23 06:07:55.645  2919  2991 D RNGcmListenerService: sendNotification
12-23 06:07:55.646  2919  2919 D com.oney.gcm.GcmModule: GCMReceiveNotification BroadcastReceiver
12-23 06:07:55.650  2919  2967 I ReactNativeJS: 'receive gcm notification', { data: 
12-23 06:07:55.650  2919  2967 I ReactNativeJS:    { info: '{"subject":"Hello GCM2","message":"Hello from the server side!"}',
12-23 06:07:55.650  2919  2967 I ReactNativeJS:      collapse_key: 'do_not_collapse' } }
12-23 06:07:55.651  2919  2967 I ReactNativeJS: 'GcmAndroid.isForground', true

And after a reload of the JS:

12-23 06:09:03.818 30699 27756 I GCM     : GCM message com.myapp 0:1450847344527411%843f15aef9fd7ecd
12-23 06:09:03.884  2919  3041 D RNGcmListenerService: sendNotification
12-23 06:09:03.891  2919  2919 D com.oney.gcm.GcmModule: GCMReceiveNotification BroadcastReceiver
12-23 06:09:03.891  2919  2919 W unknown:React: Calling JS function after bridge has been destroyed.

Hope this helps!

oney commented 8 years ago

Very useful. Thanks for helping solve the problem. The issue of reload of the JS is known to me. I will keep trying solving it.

About crash of Attached DialogModule to host with pending alert but no FragmentManager (not attached to an Activity), I think it happened in your app that is named as myapp rather than the demo project TestGcm, right?

My app still crashes after that though:

Does it mean after clicking notification the app launches and crashed?

ghost commented 8 years ago

I see you added 0.1.12. I tested the app with this and the messages inside the app work! They now get properly console logged, even when I reload the JS afterwards and send a new notification.

Basically everything works now, except that the app still crashes after I receive a notification (when the app is closed and not running in the background).

The notification and the error dialog pop up at the same time. I can dismiss the dialog, click the notification and the app starts properly. It's only the dialog showing up what's the problem now.

You're correct that I'm running this in my own app instead of the TestGcm app. If you want, I can test it and see if that one also crashes for me?

oney commented 8 years ago

Good!

Basically everything works now, except that the app still crashes after I receive a notification (when the app is closed and not running in the background).

About above issue, react native version of myapp app is 0.17.0, right?

ghost commented 8 years ago

That's correct!

I'm using the following node modules now:

"react-native": "0.17.0",
"react-native-gcm-android": "^0.1.2",
"react-native-system-notification": "0.0.7"
oney commented 8 years ago

Just update 0.1.3 to npm. It should work on react native 0.17.0. Please confirm that 5 situations are working.

Thanks!

ghost commented 8 years ago

You are awesome! Everything works.

Notification when the app is in the foreground Works - Logs the notification. isForGround = true

Notification when the app is in the background and alive Shows the notification in my status bar. Also logs it: isForGround = false

Notification when app is closed Shows the notification in my status bar.

Clicking notification will open the app Works.

Reload JS and send notifications Works.

One bug (it's a far fetched one)

Results in isForGround = false, even though the app is running.

oney commented 8 years ago

0.1.5 should solve it. Please try it!

ghost commented 8 years ago

Heyo.

Sorry for my late reply!

A use case like that would be extremely rare for my app, but perhaps other people will profit from it. Thanks for taking a look at it.

I just tested it and it does not seem to work yet. I followed the exact same steps, and I end up with GcmAndroid.isInForeground = false

This is with version 0.1.6 and react-native 0.17.0

xphaul commented 8 years ago

@oney i am experiencing this issue . it seems that if(!GcmAndroid.isInForeground) is not firing when app received a notification from and its state is on background . It is fine when the app is running . i can click the notifications