caffeinalab / ti.goosh

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

added a little support for Android Oreo #102

Closed slash-84 closed 5 years ago

emilyfeder commented 6 years ago

I believe that each notification channel can only have one configuration for sound + importance so that if your push has a custom sound it will not use that -- it will only play the sound used for the first push sent on that channel. In your changes, you are not setting the sound for the channel, so I'm guessing it plays the default sound. Did you see that behavior in your tests? See comments on this issue: https://stackoverflow.com/a/46120056/2872106

slash-84 commented 6 years ago

@emilyfeder i did not try, this is a very little support just for having notifications shown on Oreo (see #97 ) and i have not considered custom sound at all.

At the moment this PR add support just for one channel: you can only specify its name via tiapp.xml

It would be nice to completely support Oreo, with multiple channel handling and so on :)

SeanMaraj commented 5 years ago

Does this solution actually work when targeting API 26 and running on an Android 8 device? The channel implementation here seems fine but the BroadcastReceiver is still using "startWakefulService" which is no longer supported when you target 26 due to the new background restrictions. So in theory the service would never be started.

miniman42 commented 5 years ago

@SeanMaraj Doesn't look like the notifications are received if the service is not actually running :/ Did you have any luck finding a solution?

SeanMaraj commented 5 years ago

@miniman42 I'm currently migrating to Firebase using this module: https://github.com/hansemannn/titanium-firebase-cloud-messaging It uses the new Firebase services which work with the new target.

Alternatively, if you want to use continue to use this Goosh module, you'll have to compile it yourself and be sure to use "startForegroundService" instead of "startWakefulService". I did that and the service started fine. Unfortunately, I was having some trouble getting the actual notification to display properly. So that's when I decided to use the new Firebase module from Hans. It's more work to set up but Firebase seems pretty worth it.

miniman42 commented 5 years ago

@SeanMaraj Thanks for the info - I'm on a similar mission to upgrade to FCM and API level 26, we've already a fairly heavily customised version of ti.goosh :) I'll take a look at Hans module. I'm just concerned, as a foreground service is out for us. I want to ensure that a visible notification gets posted when the app is not running in the background. Hopefully there's something new in FCM that allows that? As that is why we needed the broadcast receiver up till now. Maybe I'm missing something!

SeanMaraj commented 5 years ago

@slash-84 So Hans' module by default doesn't really let you show a notification when the app is closed (unless i'm missing something). But I'm literally working on that feature right now for my app. You just have to build and show your notification in the "onMessageReceived" function in TiFirebaseMessagingService.java. No extra service is required from what I can tell.

miniman42 commented 5 years ago

Ahh yes, just took a peek at the doc's there https://developers.google.com/cloud-messaging/android/android-migrate-fcm - I'll have a go at updating my goosh to use FCM directly, looks pretty trivial #famouslastwords

emilyfeder commented 5 years ago

I ended up using the module you mentioned: https://github.com/hansemannn/titanium-firebase-cloud-messaging in my app. It did indeed have all the features I needed, including playing specific sounds for notification channels that are part of API 26, and push notifications still appear even when the app is in the background. When the app is closed, the push notification is handled by android's native fcm support, and when you click on the notification, it opens your app and any extras are part of Ti.App.Android.launchIntent.

DFoxinator commented 5 years ago

@emilyfeder did you have to do anything special with titanium-firebase-cloud-messaging to get notifications to work from the background? No matter what I do, I can't get the data to show up in Ti.App.Android.launchIntent getExtra methods. It seems to just not have access to the data that was in the payload of the notif. I also tried Ti.Android.currentActivity but the data wasn't there either. Any help is appreciated, I'm hoping to learn how you got it going.

emilyfeder commented 5 years ago

Yes, you need to do something a little different when sending the message. You need to set both a notification and data payload in the message to have the extra in the launch intent. Take a look at this issue thread in the titanium-firebase-cloud-messaging repo for details: https://github.com/hansemannn/titanium-firebase-cloud-messaging/issues/13#issuecomment-384686764

On Thu, Aug 30, 2018 at 2:36 AM David Fox notifications@github.com wrote:

@emilyfeder https://github.com/emilyfeder did you have to do anything special with titanium-firebase-cloud-messaging to get notifications to work from the background? No matter what I do, I can't get the data to show up in Ti.App.Android.launchIntent getExtra methods. It seems to just not have access to the data that was in the payload of the notif. I also tried Ti.Android.currentActivity but the data wasn't there either. Any help is appreciated, I'm hoping to learn how you got it going.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/caffeinalab/ti.goosh/pull/102#issuecomment-417220198, or mute the thread https://github.com/notifications/unsubscribe-auth/ABZk_or4oHofCsnBB2DQ8ZCFZWbKQY2dks5uV5XsgaJpZM4SXJpB .

DFoxinator commented 5 years ago

@emilyfeder thanks for the info. That's what I was trying, but it doesn't seem to work. I confirmed the data was coming through because the notification and data are successfully received if the app is in the foreground. But once in the background, the notification is received but when you navigate to the app via the notif, the data isn't showing up anywhere. It's definitely a notif + data, and works in the foreground, so not sure if I'm doing something wrong or this some kind of issue. But the data doesn't seem to be present on any of the activity/intents.

emilyfeder commented 5 years ago

Are you using the firebase console to send test messages so that you can eliminate any issues you may have in your push server? They have 'advanced' message options where you can set the data payload. Also, here's what my code looked like on the app side to grab the extra: Ti.App.Android.launchIntent.getStringExtra(key). I'm not sure beyond that what the issue may be, so I would suggest looking through the issues raised in the https://github.com/hansemannn/titanium-firebase-cloud-messaging repo.

DFoxinator commented 5 years ago

@emilyfeder thank you. I will try sending from the console. Two more questions if you don't mind - where in your app are you calling Ti.App.Android.launchIntent.getStringExtra? (I've tried numerous places, but no luck. ex. the initial window open even handler, before initial window open, etc.) I'm wondering if I'm calling it too late in the lifecycle or maybe just the wrong place, to have it not be there at the point I'm looking for it. Also, what version of the Titanium SDK are you using? I tried with various 7.x builds but no luck.

RavindraChherke commented 5 years ago

@DFoxinator

In the classic Titanium app I am getting data from intent in app.js I think for alloy app you will get it in index.js

var _intent = Ti.App.Android.launchIntent;
var chat_id = _intent.getStringExtra("chat_id");

I think your problem is related to payload. Don't forget to make necessary changes on server side. There are two section in notification payload.

  1. notifcation
  2. data
    {
     data:{
        chat_id:mychat_id
      },
     notification: {
        title:mytitle,
        body: message,
      }
    }

Please check server code given by Hans on https://github.com/hansemannn/titanium-firebase-cloud-messaging

<?php
    $url = 'https://fcm.googleapis.com/fcm/send';

    $fields = [
        'to' => '/topics/testTopic', // or device token
        'notification' => [
            'title' => 'TiFirebaseMessaging',
            'body' => 'Message received'
        ],
        'data' => [
            'key1' => 'value1',
            'key2' => 'value2'
        ]
    ];

    $headers = [
        'Authorization: key=SERVER_ID_FROM_FIREBASE_SETTIGNS_CLOUD_MESSAGING', 'Content-Type: application/json'
    ];
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

    $result = curl_exec($ch);

    echo $result;
    curl_close($ch);
?>
DFoxinator commented 5 years ago

@RavindraChherke thanks for all of the info. I am using a classic app so I have my code in app.js. I tried that code, but unfortunately, no luck. I can see the data is coming through properly, because it works if the app is in the foreground when the notif is received (didReceiveMessage) I'm able to see the passed along data. But unfortunately, when the app is backgrounded, no matter what I've tried I can't seem to get it to find any data on the intent.

Jei commented 5 years ago

We're merging this solution for now, as a starting point. We need to address the biggest problems first, and then implement a more comprehensive solution.