caffeinalab / ti.goosh

GCM/FCM module for Titanium.
MIT License
52 stars 34 forks source link

firebase notifications when app is closed #47

Closed xZhad closed 7 years ago

xZhad commented 7 years ago

I know there where reports before, but are marked as closed and I'm still experiencing the same issue

when my app is in the background and receives a notification it shows properly and i get this log

[DEBUG] D/ti.goosh.IntentService: Push notification received from: 545132955493 [DEBUG] D/ti.goosh.IntentService: Notification key : google.sent_time => 1490879315641 (java.lang.Long) [DEBUG] D/ti.goosh.IntentService: Notification key : data => {"alert":"Qwer","sound":"default","chatRoomId":"db08cfbe-ec47-4d31-8bd4-f4e308180a6b","force_show_in_foreground":true,"vibrate":true,"chatRoomName":"Ivan Sarpi","title":"Ivan Sarpi"} (java.lang.String) [DEBUG] D/ti.goosh.IntentService: Notification key : title => Ivan Sarpi (java.lang.String) [DEBUG] D/ti.goosh.IntentService: Notification key : google.message_id => 0:1490879315646921%%bbb352d4f9fd7ecd (java.lang.String) [DEBUG] D/ti.goosh.IntentService: Notification key : message => Qwer (java.lang.String)

when i click that notification i get this

[DEBUG] D/ti.goosh.TiGooshPushHandlerActivity: started [DEBUG] [TiPush]: {"inBackground":true,"data":"{\"alert\":\"Qwer\",\"sound\":\"default\",\"chatRoomId\":\"db08cfbe-ec47-4d31-8bd4-f4e308180a6b\",\"force_show_in_foreground\":true,\"vibrate\":true,\"chatRoomName\":\"Ivan Sarpi\",\"title\":\"Ivan Sarpi\"}"} [INFO] {"alert":"Qwer","sound":"default","chatRoomId":"db08cfbe-ec47-4d31-8bd4-f4e308180a6b","force_show_in_foreground":true,"vibrate":true,"chatRoomName":"Ivan Sarpi","title":"Ivan Sarpi"}

and it does what it's suppose to do (open a new window)

now, when the app is closed, i get this log when the notification arrives (it shows properly)

[DEBUG] D/ti.goosh.IntentService: Push notification received from: 545132955493 [DEBUG] D/ti.goosh.IntentService: Notification key : google.sent_time => 1490879747307 (java.lang.Long) [DEBUG] D/ti.goosh.IntentService: Notification key : data => {"alert":"Sfdac","sound":"default","chatRoomId":"db08cfbe-ec47-4d31-8bd4-f4e308180a6b","force_show_in_foreground":true,"vibrate":true,"chatRoomName":"Ivan Sarpi","title":"Ivan Sarpi"} (java.lang.String) [DEBUG] D/ti.goosh.IntentService: Notification key : title => Ivan Sarpi (java.lang.String) [DEBUG] D/ti.goosh.IntentService: Notification key : google.message_id => 0:1490879747312886%%bbb352d4f9fd7ecd (java.lang.String) [DEBUG] D/ti.goosh.IntentService: Notification key : message => Sfdac (java.lang.String)

but when i click it, it only opens the app and doesn't open the new window, it says this in the logs

[DEBUG] D/ti.goosh.TiGooshPushHandlerActivity: started [ERROR] TiApplication: (main) [45015,45015] No valid root or current activity found for application instance [INFO] TiRootActivity: (main) [0,0] checkpoint, on root activity create, savedInstanceState: null [INFO] TiApplication: (main) [1,1] Analytics have been disabled [WARN] V8Object: (KrollRuntimeThread) [235,236] Runtime disposed, cannot set property 'userAgent' [WARN] TiApplication: (KrollRuntimeThread) [5,241] Registering module with name already in use. [INFO] TiRootActivity: (main) [0,0] checkpoint, on root activity resume. activity = com.ubqti.mega.UbqtiMegaActivity@7aeb756 [DEBUG] Window: Checkpoint: postWindowCreated() [WARN] TiApplication: (KrollRuntimeThread) [115,115] Registering module with name already in use. [DEBUG] D/ti.goosh.TiGooshModule: No notification in Intent

I'm using titanium 6.0.3.GA and ti.goosh 2.0.5

thanks!

edit:

also, could it be related to this i have in my manifest?

<android xmlns:android="http://schemas.android.com/apk/res/android">
    <manifest android:versionCode="1" android:versionName="1.00">
        <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
        <uses-permission android:name="android.permission.CAMERA"/>
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
        <uses-permission android:name="android.permission.READ_CONTACTS"/>
        <application android:hardwareAccelerated="true" android:theme="@style/Light">
            <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
            <activity
                android:configChanges="keyboardHidden|orientation|screenSize"
                android:label="@string/app_name"
                android:name=".UbqtiMegaActivity"
                android:theme="@style/Theme.Titanium">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN"/>
                    <category android:name="android.intent.category.LAUNCHER"/>
                </intent-filter>
                <intent-filter>
                    <action android:name="android.intent.action.VIEW"/>
                    <category android:name="android.intent.category.DEFAULT"/>
                    <category android:name="android.intent.category.BROWSABLE"/>
                    <data android:scheme="ubqtimega"/>
                </intent-filter>
            </activity>
        </application>
    </manifest>
</android>
aitorrod commented 7 years ago

Same problem here. Did you find a solution?

bert-w commented 7 years ago

might be related to https://jira.appcelerator.org/browse/TIMOB-24316

i know there's some titanium issues regarding intents in general. Hopefully those will be fixed in 6.0.4

miniman42 commented 7 years ago

Issue is with the Modules parseBootIntent method it should be setting: if (key.equals(INTENT_EXTRA)) { notification = intent.getExtras().getString(INTENT_EXTRA); }

bert-w commented 7 years ago

can you test and create a pull request for this?

progress44 commented 7 years ago

The issue @Bertuzb mentioned seems related. The pull request for the fix includes this commit https://github.com/appcelerator/titanium_mobile/pull/8910/commits/dddb6a3a2f1803abd60451e3600564943c3d08b4 The condition above should be correct instead

if (key.equals(INTENT_EXTRA)) { notification = intent.getExtras().getString(INTENT_EXTRA); }
bert-w commented 7 years ago

im running 6.0.4 now though but I still cant seem to receive notification data in-app (after clicking notification) while having the app in background

progress44 commented 7 years ago

I'll make some tests shortly

bert-w commented 7 years ago

https://gist.github.com/Bertuzb/217b080b935aab362c72b213c3625368

its the testcase I used. Basically, the focus event on the main window is called when resuming from background. It should contain some data but it currently doesnt. Hope it helps

progress44 commented 7 years ago

[x] App closed: Notification is received and callback is fired when tapped on it [x] App in background: Notification is received and callback is fired [x] App in foreground: Callback is fired

Tested with SDK 6.0.0.GA and all cases still work.

progress44 commented 7 years ago

You should always call registerForPushNotifications because it initialises the module with the callback functions.

$.index.open();

var TiGoosh = require('ti.goosh');
TiGoosh.registerForPushNotifications({
    // The callback to invoke when a notification arrives.
    callback: function(e) {
        alert(e.data);
        /*var data = JSON.parse(e.data || '');
        Ti.API.debug("Callback", data);*/
    },

    // The callback invoked when you have the device token.
    success: function(e) {

        // Send the e.deviceToken variable to your PUSH server
        Ti.API.debug('Notifications: device token is ' + e.deviceToken);

    },

    // The callback invoked on some errors.
    error: function(err) {
        Ti.API.error('Notifications: Retrieve device token failed', err);
    }
});
bert-w commented 7 years ago

can you test the additional data attribute? I am also receiving the notification when the app is in background but when clicking it, the app does not receive any data.

If you could provide your testcase that would be awesome too.

I've loaded the register function of TiGoosh immediately upon startup but it does not make a change in comparison to clicking a label and firing it that way

bert-w commented 7 years ago

Also that first comment confused me, a fork of 6_0_X with "some new features" but last line says you tested with 6.0.0.GA

progress44 commented 7 years ago

I tested the module with both our fork of the sdk 6.0.4.GA and the official sdk 6.0.0.GA and it works every time. The code I'm using is the one I provided in the comment above. alert(e.data) is correctly displayed in each case

bert-w commented 7 years ago

what do you do exactly with the google play services? I notice i get an error stating

[ERROR] GooglePlayServicesUtil: The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.

but i am not sure how to install these and how to make it work with various android devices. Until now I was under the assumption this did not affect this issue, but perhaps it does?

progress44 commented 7 years ago

The message is obsolete. It's a bug in google play services but doesn't impact on the module. It works nonetheless

bert-w commented 7 years ago

last thing i can find is this error im getting:

05-04 13:05:38.802: E/Notification(2383): setLatestEventInfo() is deprecated and you should feel deprecated.
05-04 13:05:38.802: E/Notification(2383): java.lang.Throwable
05-04 13:05:38.802: E/Notification(2383):   at android.app.Notification.setLatestEventInfo(Notification.java:2083)
05-04 13:05:38.802: E/Notification(2383):   at com.android.server.am.ActivityManagerService$MainHandler.handleMessage(ActivityManagerService.java:2352)
05-04 13:05:38.802: E/Notification(2383):   at android.os.Handler.dispatchMessage(Handler.java:102)
05-04 13:05:38.802: E/Notification(2383):   at android.os.Looper.loop(Looper.java:154)
05-04 13:05:38.802: E/Notification(2383):   at android.os.HandlerThread.run(HandlerThread.java:61)
05-04 13:05:38.802: E/Notification(2383):   at com.android.server.ServiceThread.run(ServiceThread.java:46)

Im using a Samsung Galaxy S3 but its running Android 7.1 (LineageOS). I have also tried on an Android 6.0.1 device but it does not throw the above error. Both devices will not call the callback even though its registered on startup.

Can you post the tiapp manifest?

bert-w commented 7 years ago

hold on im experiencing some differences between the api endpoints

https://android.googleapis.com/gcm/send (old one which this module implies) and https://fcm.googleapis.com/fcm/send (new one which i use)

I am getting correct callback events indeed on the gcm variant

bert-w commented 7 years ago

well this seems to have something to do with https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages .

The payload im sending to POST https://fcm.googleapis.com/fcm/send is:

$data = json_encode(
      [
        $sendKey => $to,
        'notification' => ..., // title and body as seen in https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support
        'data' => [
          'data' => $data // some custom data
        ]
      ]
    );

Removing the notification key causes the use of data key to form a notification. The data key must then contain alert (and preferabbly also title). Defining notification takes precedence though.

The notification object behaves like (quote): "FCM automatically displays the message to end-user devices on behalf of the client app. Notification messages have a predefined set of user-visible keys and an optional data payload of custom key-value pairs."

I will try some tests with the data object, but I have to make it work for iOS devices as well (they use the same firebase service).

progress44 commented 7 years ago

Yeah that's true. I remember, I answered this in a previous issue. notification and data don't work well together. Check out https://github.com/caffeinalab/ti.goosh/blob/master/test-fcm to see a simple implementation

bert-w commented 7 years ago

ok thanks for the effort you put into this. I will try to convert my api call to use the data-setup, but i might receive conflicts on the iOS side.

It might be a good idea as well to put a notice somewhere in the readme, that points the difference out vs notification and data. The TiGoosh module states GCM/FCM support after all but it has some gotcha's

progress44 commented 7 years ago

Yeah true. We'll update the documentation. Thanks for the feedback

zulfiandri commented 7 years ago

hallo , I have problem

I use SDK: 6.0.4 , Appcelerator Studio build: 4.8.1 ti.goosh: 2.0.6 device: Lenovo k4 note a7010 [x] App closed: Notification is received and callback is fired when tapped on it [x] App in foreground: Callback is fired

My problem is : [x] App in background: Notification is received and callback not fired

Lenndev commented 7 years ago

@zulfiandri still encountering this problem?