0

please note that problem might be related to Android 10

Im trying to start a new Activity from myInAppMessagingService, but i got null pointer exception un startActivitys context parameter every time.

So here is my Service code :

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.NonNull;

import com.google.firebase.inappmessaging.FirebaseInAppMessagingClickListener;
import com.google.firebase.inappmessaging.model.Action;
import com.google.firebase.inappmessaging.model.CampaignMetadata;
import com.google.firebase.inappmessaging.model.InAppMessage;

import viaapp_v2.systems.webview_activity.webview_base;

public class MyFirebaseInAppMessaging extends Service implements FirebaseInAppMessagingClickListener {

    String TAG = "MyFirebaseInAppMessaging";

    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void messageClicked(@NonNull InAppMessage inAppMessage, @NonNull Action action) {
        // Determine which URL the user clicked
        String url = action.getActionUrl();
        Log.d(TAG, "Popup URL :"+url);

        // Get general information about the campaign
        CampaignMetadata metadata = inAppMessage.getCampaignMetadata();
        Log.d(TAG, "metadata :"+metadata);

        try{
            startActivity(
                    new Intent(MyFirebaseInAppMessaging.this, webview_base.class)
                            .putExtra("web_url", url)
            );
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

I got error in "startActivity(..)" line because of 1st parameter, context. I tried everything - getApplicationContext(), MyFirebaseInAppMessaging.this or just simply "this" but nothing works.

I read a restrictions provided by Android Developers, but i couldnt figure out anything new.

Otherwise the app works perfectly - webview_base class works as it should, so does everything else, including myInAppMesaging Services listener. Its just that one context in startActivity() which stops me.

Thanks for any help.

--Update on Sep 7

After playing around with permissions, flags ect. i noticed that nothing works. Newer Android OS opens an web overly over the app, but older Android OS just crashes without any specific crash report. Thats weird.

Maris S.
  • 51
  • 1
  • 12
  • If `this` does not work, then your code snippet is not a method in a `Service`. I recommend that you post the entire service class, not just one method from one nested object inside of it. – CommonsWare Sep 05 '20 at 22:40
  • @CommonsWare Oh, sorry, i updated the post with the full code from service class. – Maris S. Sep 05 '20 at 22:47
  • You should be able to just use `this`, in terms of your apparent compile error. At runtime, though, this will not work on Android 10+. Please display a notification! – CommonsWare Sep 05 '20 at 23:10

3 Answers3

0

Try below code , it will work..

you have to add FLAG- FLAG_ACTIVITY_NEW_TASK

Intent myIntent = new Intent(this, webview_base.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(myIntent);
Ravindra Bagale
  • 17,226
  • 9
  • 43
  • 70
  • Just tried that, it didnt work. But i should use it there anyway to support devices under Android 10, right ? – Maris S. Sep 05 '20 at 22:42
0

If your app is not visible and has not been visible for a while and you don't want to use a notification, it will not be allowed in Android 10, except if

The app has been granted the SYSTEM_ALERT_WINDOW permission by the user.

(From the restrictions page mentioned in the question)

So that's an option for Android 10.

francis duvivier
  • 2,056
  • 11
  • 17
  • I added ALERt_WINDOW permission in manifest, but i couldnt get the popup URL working anyways. Maybe you can provide an example or something else that could lead me into right path of coding. – Maris S. Sep 07 '20 at 16:00
  • Maybe i missed the part "granted" of permission which also means that i have to request user permissions ? – Maris S. Sep 07 '20 at 16:00
  • Yes, read the documentation from the link, it's even more strict because the user needs to manually activate it. – francis duvivier Sep 07 '20 at 16:04
0

It's not enough to list the SYSTEM_ALERT_WINDOW in AndroidManifest.xml. You also need user to give the app "Draw over other apps" permission. Check here how to do that easily.

Václav Hodek
  • 638
  • 4
  • 9