put up toast when FCM message received

It's too useful to live without. So: tap into the static list of live
Delegates, and if any of them has an Activity available use it to run
Toast on UI thread. Otherwise there will be no display.
This commit is contained in:
Eric House 2019-02-01 16:49:12 -08:00
parent fc0480376b
commit f28c21206c
3 changed files with 44 additions and 10 deletions

View file

@ -141,11 +141,13 @@ public class DelegateBase implements DlgClickNotify,
protected void onStart()
{
Class clazz = getClass();
if ( s_instances.containsKey( clazz ) ) {
Log.d( TAG, "onStart(): replacing curThis" );
synchronized (s_instances) {
Class clazz = getClass();
if ( s_instances.containsKey( clazz ) ) {
Log.d( TAG, "onStart(): replacing curThis" );
}
s_instances.put( clazz, new WeakReference<DelegateBase>(this) );
}
s_instances.put( clazz, new WeakReference<DelegateBase>(this) );
}
protected void onResume()
@ -165,7 +167,10 @@ public class DelegateBase implements DlgClickNotify,
protected DelegateBase curThis()
{
DelegateBase result = null;
WeakReference<DelegateBase> ref = s_instances.get( getClass() );
WeakReference<DelegateBase> ref;
synchronized (s_instances) {
ref = s_instances.get( getClass() );
}
if ( null != ref ) {
result = ref.get();
}
@ -777,4 +782,20 @@ public class DelegateBase implements DlgClickNotify,
{
// Assert.fail();
}
public static Activity getHasLooper()
{
Activity result = null;
synchronized (s_instances) {
for ( WeakReference<DelegateBase> ref : s_instances.values() ) {
DelegateBase base = ref.get();
if ( null != base ) {
result = base.getActivity();
break;
}
}
}
Log.d( TAG, "getHasLooper() => %s", result );
return result;
}
}

View file

@ -159,13 +159,22 @@ public class Utils {
showToast( context, text );
}
public static void showToast( Context context, String msg )
public static void showToast( final Context context,
final String msg )
{
// Make this safe to call from non-looper threads
try {
Toast.makeText( context, msg, Toast.LENGTH_SHORT).show();
} catch ( java.lang.RuntimeException re ) {
Log.ex( TAG, re );
Activity activity = DelegateBase.getHasLooper();
if ( null != activity ) {
activity.runOnUiThread( new Runnable() {
@Override
public void run() {
try {
Toast.makeText( context, msg, Toast.LENGTH_SHORT).show();
} catch ( java.lang.RuntimeException re ) {
Log.ex( TAG, re );
}
}
} );
}
}

View file

@ -58,6 +58,10 @@ public class FBMService extends FirebaseMessagingService {
} else {
RelayService.fcmConfirmed( this, true );
if ( BuildConfig.DEBUG ) {
Utils.showToast( this, TAG + ".onMessageReceived()" );
}
Map<String, String> data = message.getData();
Log.d( TAG, "onMessageReceived(data=%s)", data );