31
31
import android .app .NotificationManager ;
32
32
import android .content .Context ;
33
33
import android .content .SharedPreferences ;
34
+ import android .os .AsyncTask ;
34
35
import android .os .Bundle ;
35
36
import android .os .Looper ;
36
37
import android .util .Log ;
37
38
import android .view .Menu ;
38
39
import android .view .MenuItem ;
39
40
import android .view .View ;
40
41
import android .widget .Button ;
42
+ import android .widget .CheckBox ;
43
+ import android .widget .CompoundButton ;
41
44
import android .widget .TextView ;
42
45
import android .os .Handler ;
46
+ import android .widget .Toast ;
43
47
44
48
import com .onesignal .OneSignal .NotificationOpenedHandler ;
45
49
@@ -56,13 +60,26 @@ public class MainActivity extends Activity implements OSEmailSubscriptionObserve
56
60
57
61
IabHelper mHelper ;
58
62
59
- private int [] interactiveViewIds = new int [] {com .onesignal .example .R .id .subscribe , com .onesignal .example .R .id .unsubscribe , com .onesignal .example .R .id .sendTags , com .onesignal .example .R .id .getTags , com .onesignal .example .R .id .setEmail };
63
+ private int [] interactiveViewIds = new int []{
64
+ R .id .subscribe ,
65
+ R .id .unsubscribe ,
66
+ R .id .sendTags ,
67
+ R .id .getTags ,
68
+ R .id .setEmail ,
69
+ R .id .postNotification ,
70
+ R .id .postNotificationAsync ,
71
+ R .id .postNotificationGroupCheckBox ,
72
+ R .id .postNotificationAsyncGroupCheckBox };
60
73
61
74
private TextView debugTextView ;
62
75
private TextView emailTextView ;
63
76
private Button consentButton ;
64
77
private Button setEmailButton ;
65
78
private Button logoutEmailButton ;
79
+ private Button postNotifButton ;
80
+ private Button postNotifAsyncButton ;
81
+ private CheckBox postNotifGroupCheckBox ;
82
+ private CheckBox postNotifAsyncGroupCheckBox ;
66
83
67
84
private int sendTagsCounter = 1 ;
68
85
private boolean addedObservers = false ;
@@ -71,14 +88,39 @@ public class MainActivity extends Activity implements OSEmailSubscriptionObserve
71
88
private TextView triggerValueTextView ;
72
89
private String SHARDPRES_KEY_IAM_HOST = "SHARDPRES_KEY_IAM_HOST" ;
73
90
91
+ @ Override
92
+ public boolean onCreateOptionsMenu (Menu menu ) {
93
+ // Inflate the menu; this adds items to the action bar if it is present.
94
+ getMenuInflater ().inflate (com .onesignal .example .R .menu .menu_main , menu );
95
+ return true ;
96
+ }
97
+
98
+ @ Override
99
+ public boolean onOptionsItemSelected (MenuItem item ) {
100
+ // Handle action bar item clicks here. The action bar will
101
+ // automatically handle clicks on the Home/Up button, so long
102
+ // as you specify a parent activity in AndroidManifest.xml.
103
+ int id = item .getItemId ();
104
+
105
+ //noinspection SimplifiableIfStatement
106
+ if (id == R .id .action_settings )
107
+ return true ;
108
+
109
+ return super .onOptionsItemSelected (item );
110
+ }
111
+
74
112
@ Override
75
113
protected void onCreate (Bundle savedInstanceState ) {
76
114
super .onCreate (savedInstanceState );
77
115
setContentView (com .onesignal .example .R .layout .activity_main );
78
116
79
- this .consentButton = (Button )this .findViewById (com .onesignal .example .R .id .consentButton );
80
- this .setEmailButton = (Button )this .findViewById (com .onesignal .example .R .id .setEmail );
81
- this .logoutEmailButton = (Button )this .findViewById (com .onesignal .example .R .id .logoutEmail );
117
+ this .consentButton = this .findViewById (R .id .consentButton );
118
+ this .setEmailButton = this .findViewById (R .id .setEmail );
119
+ this .logoutEmailButton = this .findViewById (R .id .logoutEmail );
120
+ this .postNotifButton = this .findViewById (R .id .postNotification );
121
+ this .postNotifAsyncButton = this .findViewById (R .id .postNotificationAsync );
122
+ this .postNotifGroupCheckBox = this .findViewById (R .id .postNotificationGroupCheckBox );
123
+ this .postNotifAsyncGroupCheckBox = this .findViewById (R .id .postNotificationAsyncGroupCheckBox );
82
124
this .iamHost = this .findViewById (R .id .iamHost );
83
125
this .triggerKeyTextView = this .findViewById (R .id .triggerKey );
84
126
this .triggerValueTextView = this .findViewById (R .id .triggerValue );
@@ -115,6 +157,8 @@ public void onIabSetupFinished(IabResult result) {
115
157
// Hooray, IAB is fully set up!
116
158
}
117
159
});
160
+
161
+ setupGroupingNotificationCheckBoxes ();
118
162
}
119
163
120
164
private void updateIamhost () {
@@ -294,41 +338,115 @@ public void onFullScreenClicked(View v) {
294
338
updateIamhost ();
295
339
}
296
340
297
- @ Override
298
- public boolean onCreateOptionsMenu (Menu menu ) {
299
- // Inflate the menu; this adds items to the action bar if it is present.
300
- getMenuInflater ().inflate (com .onesignal .example .R .menu .menu_main , menu );
301
- return true ;
302
- }
341
+ public void onPostNotifClicked (View v ) {
303
342
304
- @ Override
305
- public boolean onOptionsItemSelected (MenuItem item ) {
306
- // Handle action bar item clicks here. The action bar will
307
- // automatically handle clicks on the Home/Up button, so long
308
- // as you specify a parent activity in AndroidManifest.xml.
309
- int id = item .getItemId ();
343
+ String userId = OneSignal .getUserId ();
310
344
311
- //noinspection SimplifiableIfStatement
312
- if (id == R .id .action_settings )
313
- return true ;
345
+ JSONObject notifPayload = null ;
346
+ try {
314
347
315
- return super .onOptionsItemSelected (item );
348
+ notifPayload = new JSONObject ("{'contents': " + "{'en':'Test Message'}, 'include_player_ids': ['" + userId + "']}" );
349
+
350
+ if (postNotifGroupCheckBox .isChecked ())
351
+ notifPayload .put ("android_group" , "group_1" );
352
+
353
+ } catch (JSONException e ) {
354
+ e .printStackTrace ();
355
+ }
356
+
357
+ OneSignal .postNotification (notifPayload , new OneSignal .PostNotificationResponseHandler () {
358
+ @ Override
359
+ public void onSuccess (JSONObject response ) {
360
+ OneSignal .onesignalLog (OneSignal .LOG_LEVEL .DEBUG , response .toString ());
361
+ }
362
+
363
+ @ Override
364
+ public void onFailure (JSONObject response ) {
365
+ OneSignal .onesignalLog (OneSignal .LOG_LEVEL .ERROR , response .toString ());
366
+ }
367
+ });
316
368
}
317
369
318
- // NotificationOpenedHandler is implemented in its own class instead of adding implements to MainActivity so we don't hold on to a reference of our first activity if it gets recreated.
319
- private class ExampleNotificationOpenedHandler implements NotificationOpenedHandler {
320
- /**
321
- * Callback to implement in your app to handle when a notification is opened from the Android status bar or
322
- * a new one comes in while the app is running.
323
- * This method is located in this activity as an example, you may have any class you wish implement NotificationOpenedHandler and define this method.
324
- *
325
- * @param openedResult The message string the user seen/should see in the Android status bar.
326
- */
370
+ public void onPostNotifAsyncClicked (View v ) {
371
+ new AsyncTaskRunner ().execute (
372
+ postNotifAsyncGroupCheckBox .isChecked ()
373
+ );
374
+ }
375
+
376
+ private class AsyncTaskRunner extends AsyncTask <Object , Void , Void > {
377
+
378
+ @ Override
379
+ protected Void doInBackground (Object ... params ) {
380
+
381
+ try {
382
+ Thread .sleep (1000 );
383
+ } catch (InterruptedException e ) {
384
+ e .printStackTrace ();
385
+ }
386
+
387
+ String userId = OneSignal .getUserId ();
388
+
389
+ JSONObject notifPayload = null ;
390
+ try {
391
+
392
+ notifPayload = new JSONObject ("{'contents': " + "{'en':'Test Message'}, 'include_player_ids': ['" + userId + "']}" );
393
+
394
+ Boolean addGroup = (Boolean ) params [0 ];
395
+ if (addGroup )
396
+ notifPayload .put ("android_group" , "group_1" );
397
+
398
+ } catch (JSONException e ) {
399
+ e .printStackTrace ();
400
+ }
401
+
402
+ OneSignal .postNotification (notifPayload , new OneSignal .PostNotificationResponseHandler () {
403
+ @ Override
404
+ public void onSuccess (JSONObject response ) {
405
+ OneSignal .onesignalLog (OneSignal .LOG_LEVEL .DEBUG , response .toString ());
406
+ }
407
+
408
+ @ Override
409
+ public void onFailure (JSONObject response ) {
410
+ OneSignal .onesignalLog (OneSignal .LOG_LEVEL .ERROR , response .toString ());
411
+ }
412
+ });
413
+
414
+ return null ;
415
+ }
416
+
417
+
327
418
@ Override
328
- public void notificationOpened (OSNotificationOpenResult openedResult ) {
329
- Log .e ("OneSignalExample" , "body: " + openedResult .notification .payload .body );
330
- Log .e ("OneSignalExample" , "additional data: " + openedResult .notification .payload .additionalData );
331
- //Log.e("OneSignalExample", "additionalData: " + additionalData.toString());
419
+ protected void onPostExecute (Void result ) {
420
+
421
+ }
422
+
423
+
424
+ @ Override
425
+ protected void onPreExecute () {
426
+
427
+ }
428
+
429
+
430
+ @ Override
431
+ protected void onProgressUpdate (Void ... entry ) {
432
+
332
433
}
333
434
}
435
+
436
+ private void setupGroupingNotificationCheckBoxes () {
437
+ this .postNotifGroupCheckBox .setOnCheckedChangeListener (new CompoundButton .OnCheckedChangeListener () {
438
+ @ Override
439
+ public void onCheckedChanged (CompoundButton buttonView , boolean isChecked ) {
440
+ Toaster .makeToast (MainActivity .this , "Main thread Notifications will be grouped: " + isChecked , Toast .LENGTH_SHORT );
441
+ }
442
+ });
443
+
444
+ this .postNotifAsyncGroupCheckBox .setOnCheckedChangeListener (new CompoundButton .OnCheckedChangeListener () {
445
+ @ Override
446
+ public void onCheckedChanged (CompoundButton buttonView , boolean isChecked ) {
447
+ Toaster .makeToast (MainActivity .this , "Async notifications will be grouped: " + isChecked , Toast .LENGTH_SHORT );
448
+ }
449
+ });
450
+ }
451
+
334
452
}
0 commit comments