11package com .microsoft .azure .mobile .sasquatch .activities ;
22
3+ import android .app .Activity ;
34import android .content .Context ;
45import android .content .DialogInterface ;
56import android .content .Intent ;
67import android .content .SharedPreferences ;
78import android .os .Bundle ;
89import android .os .StrictMode ;
10+ import android .support .annotation .NonNull ;
911import android .support .annotation .Nullable ;
1012import android .support .annotation .VisibleForTesting ;
1113import android .support .test .espresso .idling .CountingIdlingResource ;
2022import android .widget .Toast ;
2123
2224import com .microsoft .azure .mobile .MobileCenter ;
23- import com .microsoft .azure .mobile .MobileCenterService ;
2425import com .microsoft .azure .mobile .ResultCallback ;
2526import com .microsoft .azure .mobile .analytics .Analytics ;
2627import com .microsoft .azure .mobile .analytics .AnalyticsPrivateHelper ;
3233import com .microsoft .azure .mobile .crashes .model .ErrorReport ;
3334import com .microsoft .azure .mobile .distribute .Distribute ;
3435import com .microsoft .azure .mobile .ingestion .models .LogWithProperties ;
36+ import com .microsoft .azure .mobile .push .Push ;
37+ import com .microsoft .azure .mobile .push .PushListener ;
38+ import com .microsoft .azure .mobile .push .PushNotification ;
3539import com .microsoft .azure .mobile .sasquatch .R ;
3640import com .microsoft .azure .mobile .sasquatch .SasquatchDistributeListener ;
37- import com .microsoft .azure .mobile .sasquatch .features .PushListenerHelper ;
3841import com .microsoft .azure .mobile .sasquatch .features .TestFeatures ;
3942import com .microsoft .azure .mobile .sasquatch .features .TestFeaturesListAdapter ;
4043import com .microsoft .azure .mobile .utils .MobileCenterLog ;
4144
4245import org .json .JSONObject ;
4346
47+ import java .util .Map ;
48+
4449public class MainActivity extends AppCompatActivity {
4550
4651 public static final String LOG_TAG = "MobileCenterSasquatch" ;
@@ -71,7 +76,7 @@ protected void onCreate(Bundle savedInstanceState) {
7176 AnalyticsPrivateHelper .setListener (getAnalyticsListener ());
7277 Crashes .setListener (getCrashesListener ());
7378 Distribute .setListener (new SasquatchDistributeListener ());
74- PushListenerHelper . setup ( );
79+ Push . setListener ( getPushListener () );
7580
7681 /* Set distribute urls. */
7782 String installUrl = getString (R .string .install_url );
@@ -83,33 +88,13 @@ protected void onCreate(Bundle savedInstanceState) {
8388 Distribute .setApiUrl (apiUrl );
8489 }
8590
86- /* Get push module reference in project build flavour. */
87- Class <? extends MobileCenterService > push = null ;
88- try {
89- //noinspection unchecked
90- push = (Class <? extends MobileCenterService >) Class .forName ("com.microsoft.azure.mobile.push.Push" );
91- } catch (Exception e ) {
92- MobileCenterLog .warn (LOG_TAG , "Push class not yet available in this flavor." );
93- }
94-
9591 /* Enable Firebase analytics if we enabled the setting previously. */
96- if (push != null && sSharedPreferences .getBoolean (FIREBASE_ENABLED_KEY , false )) {
97- try {
98- push .getMethod ("enableFirebaseAnalytics" , Context .class ).invoke (null , this );
99- MobileCenterLog .info (LOG_TAG , "Enabled firebase analytics." );
100- } catch (Exception e ) {
101- MobileCenterLog .error (LOG_TAG , "Failed to enable firebase analytics." , e );
102- }
92+ if (sSharedPreferences .getBoolean (FIREBASE_ENABLED_KEY , false )) {
93+ Push .enableFirebaseAnalytics (this );
10394 }
10495
10596 /* Start Mobile center. */
106- MobileCenter .start (getApplication (), sSharedPreferences .getString (APP_SECRET_KEY , getString (R .string .app_secret )), Analytics .class , Crashes .class , Distribute .class );
107- if (push != null )
108- try {
109- MobileCenter .start (push );
110- } catch (Exception e ) {
111- MobileCenterLog .error (LOG_TAG , "Failed to start push." , e );
112- }
97+ MobileCenter .start (getApplication (), sSharedPreferences .getString (APP_SECRET_KEY , getString (R .string .app_secret )), Analytics .class , Crashes .class , Distribute .class , Push .class );
11398
11499 /* Print last crash. */
115100 Log .i (LOG_TAG , "Crashes.hasCrashedInLastSession=" + Crashes .hasCrashedInLastSession ());
@@ -196,9 +181,8 @@ public void onSendingFailed(ErrorReport report, Exception e) {
196181 }
197182
198183 @ Override
184+ @ SuppressWarnings ("ThrowableResultOfMethodCallIgnored" )
199185 public void onSendingSucceeded (ErrorReport report ) {
200-
201- @ SuppressWarnings ("ThrowableResultOfMethodCallIgnored" )
202186 String message = String .format ("%s\n Crash ID: %s" , getString (R .string .crash_sent_succeeded ), report .getId ());
203187 if (report .getThrowable () != null ) {
204188 message += String .format ("\n Throwable: %s" , report .getThrowable ().toString ());
@@ -255,4 +239,30 @@ public void onSendingSucceeded(com.microsoft.azure.mobile.ingestion.models.Log l
255239 }
256240 };
257241 }
242+
243+ @ NonNull
244+ private PushListener getPushListener () {
245+ return new PushListener () {
246+
247+ @ Override
248+ public void onPushNotificationReceived (Activity activity , PushNotification pushNotification ) {
249+ String title = pushNotification .getTitle ();
250+ String message = pushNotification .getMessage ();
251+ Map <String , String > customData = pushNotification .getCustomData ();
252+ MobileCenterLog .info (MainActivity .LOG_TAG , "Push received title=" + title + " message=" + message + " customData=" + customData + " activity=" + activity );
253+ if (message != null ) {
254+ android .app .AlertDialog .Builder dialog = new android .app .AlertDialog .Builder (activity );
255+ dialog .setTitle (title );
256+ dialog .setMessage (message );
257+ if (!customData .isEmpty ()) {
258+ dialog .setMessage (message + "\n " + customData );
259+ }
260+ dialog .setPositiveButton (android .R .string .ok , null );
261+ dialog .show ();
262+ } else {
263+ Toast .makeText (activity , String .format (activity .getString (R .string .push_toast ), customData ), Toast .LENGTH_LONG ).show ();
264+ }
265+ }
266+ };
267+ }
258268}
0 commit comments