This repository has been archived by the owner on Sep 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 113
/
MainActivity.java
844 lines (672 loc) · 40.2 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
/*
Copyright 2016 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.example.android.wearable.wear.wearnotifications;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.BigPictureStyle;
import android.support.v4.app.NotificationCompat.BigTextStyle;
import android.support.v4.app.NotificationCompat.InboxStyle;
import android.support.v4.app.NotificationCompat.MessagingStyle;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.app.RemoteInput;
import android.support.v4.app.TaskStackBuilder;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import com.example.android.wearable.wear.common.mock.MockDatabase;
import com.example.android.wearable.wear.common.util.NotificationUtil;
import com.example.android.wearable.wear.wearnotifications.handlers.BigPictureSocialIntentService;
import com.example.android.wearable.wear.wearnotifications.handlers.BigPictureSocialMainActivity;
import com.example.android.wearable.wear.wearnotifications.handlers.BigTextIntentService;
import com.example.android.wearable.wear.wearnotifications.handlers.BigTextMainActivity;
import com.example.android.wearable.wear.wearnotifications.handlers.InboxMainActivity;
import com.example.android.wearable.wear.wearnotifications.handlers.MessagingIntentService;
import com.example.android.wearable.wear.wearnotifications.handlers.MessagingMainActivity;
/**
* The Activity demonstrates several popular Notification.Style examples along with their best
* practices (include proper Android Wear support when you don't have a dedicated Android Wear
* app).
*/
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
public static final String TAG = "MainActivity";
public static final int NOTIFICATION_ID = 888;
// Used for Notification Style array and switch statement for Spinner selection.
private static final String BIG_TEXT_STYLE = "BIG_TEXT_STYLE";
private static final String BIG_PICTURE_STYLE = "BIG_PICTURE_STYLE";
private static final String INBOX_STYLE = "INBOX_STYLE";
private static final String MESSAGING_STYLE = "MESSAGING_STYLE";
// Collection of notification styles to back ArrayAdapter for Spinner.
private static final String[] NOTIFICATION_STYLES =
{BIG_TEXT_STYLE, BIG_PICTURE_STYLE, INBOX_STYLE, MESSAGING_STYLE};
private static final String[] NOTIFICATION_STYLES_DESCRIPTION =
{
"Demos reminder type app using BIG_TEXT_STYLE",
"Demos social type app using BIG_PICTURE_STYLE + inline notification response",
"Demos email type app using INBOX_STYLE",
"Demos messaging app using MESSAGING_STYLE + inline notification responses"
};
private NotificationManagerCompat mNotificationManagerCompat;
private int mSelectedNotification = 0;
// RelativeLayout required for SnackBars to alert users when Notifications are disabled for app.
private RelativeLayout mMainRelativeLayout;
private Spinner mSpinner;
private TextView mNotificationDetailsTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMainRelativeLayout = (RelativeLayout) findViewById(R.id.mainRelativeLayout);
mNotificationDetailsTextView = (TextView) findViewById(R.id.notificationDetails);
mSpinner = (Spinner) findViewById(R.id.spinner);
mNotificationManagerCompat = NotificationManagerCompat.from(getApplicationContext());
// Create an ArrayAdapter using the string array and a default spinner layout.
ArrayAdapter<CharSequence> adapter =
new ArrayAdapter(
this,
android.R.layout.simple_spinner_item,
NOTIFICATION_STYLES);
// Specify the layout to use when the list of choices appears.
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner.
mSpinner.setAdapter(adapter);
mSpinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG, "onItemSelected(): position: " + position + " id: " + id);
mSelectedNotification = position;
mNotificationDetailsTextView.setText(
NOTIFICATION_STYLES_DESCRIPTION[mSelectedNotification]);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// Required
}
public void onClick(View view) {
Log.d(TAG, "onClick()");
boolean areNotificationsEnabled = mNotificationManagerCompat.areNotificationsEnabled();
if (!areNotificationsEnabled) {
// Because the user took an action to create a notification, we create a prompt to let
// the user re-enable notifications for this application again.
Snackbar snackbar = Snackbar
.make(
mMainRelativeLayout,
"You need to enable notifications for this app",
Snackbar.LENGTH_LONG)
.setAction("ENABLE", new View.OnClickListener() {
@Override
public void onClick(View view) {
// Links to this app's notification settings
openNotificationSettingsForApp();
}
});
snackbar.show();
return;
}
String notificationStyle = NOTIFICATION_STYLES[mSelectedNotification];
switch (notificationStyle) {
case BIG_TEXT_STYLE:
generateBigTextStyleNotification();
break;
case BIG_PICTURE_STYLE:
generateBigPictureStyleNotification();
break;
case INBOX_STYLE:
generateInboxStyleNotification();
break;
case MESSAGING_STYLE:
generateMessagingStyleNotification();
break;
default:
// continue below
}
}
/*
* Generates a BIG_TEXT_STYLE Notification that supports both phone/tablet and wear. For devices
* on API level 16 (4.1.x - Jelly Bean) and after, displays BIG_TEXT_STYLE. Otherwise, displays
* a basic notification.
*/
private void generateBigTextStyleNotification() {
Log.d(TAG, "generateBigTextStyleNotification()");
// Main steps for building a BIG_TEXT_STYLE notification:
// 0. Get your data
// 1. Create/Retrieve Notification Channel for O and beyond devices (26+)
// 2. Build the BIG_TEXT_STYLE
// 3. Set up main Intent for notification
// 4. Create additional Actions for the Notification
// 5. Build and issue the notification
// 0. Get your data (everything unique per Notification).
MockDatabase.BigTextStyleReminderAppData bigTextStyleReminderAppData =
MockDatabase.getBigTextStyleData();
// 1. Create/Retrieve Notification Channel for O and beyond devices (26+).
String notificationChannelId =
NotificationUtil.createNotificationChannel(this, bigTextStyleReminderAppData);
// 2. Build the BIG_TEXT_STYLE.
BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle()
// Overrides ContentText in the big form of the template.
.bigText(bigTextStyleReminderAppData.getBigText())
// Overrides ContentTitle in the big form of the template.
.setBigContentTitle(bigTextStyleReminderAppData.getBigContentTitle())
// Summary line after the detail section in the big form of the template.
// Note: To improve readability, don't overload the user with info. If Summary Text
// doesn't add critical information, you should skip it.
.setSummaryText(bigTextStyleReminderAppData.getSummaryText());
// 3. Set up main Intent for notification.
Intent notifyIntent = new Intent(this, BigTextMainActivity.class);
// When creating your Intent, you need to take into account the back state, i.e., what
// happens after your Activity launches and the user presses the back button.
// There are two options:
// 1. Regular activity - You're starting an Activity that's part of the application's
// normal workflow.
// 2. Special activity - The user only sees this Activity if it's started from a
// notification. In a sense, the Activity extends the notification by providing
// information that would be hard to display in the notification itself.
// For the BIG_TEXT_STYLE notification, we will consider the activity launched by the main
// Intent as a special activity, so we will follow option 2.
// For an example of option 1, check either the MESSAGING_STYLE or BIG_PICTURE_STYLE
// examples.
// For more information, check out our dev article:
// https://developer.android.com/training/notify-user/navigation.html
// Sets the Activity to start in a new, empty task
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent notifyPendingIntent =
PendingIntent.getActivity(
this,
0,
notifyIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
// 4. Create additional Actions (Intents) for the Notification.
// In our case, we create two additional actions: a Snooze action and a Dismiss action.
// Snooze Action.
Intent snoozeIntent = new Intent(this, BigTextIntentService.class);
snoozeIntent.setAction(BigTextIntentService.ACTION_SNOOZE);
PendingIntent snoozePendingIntent = PendingIntent.getService(this, 0, snoozeIntent, 0);
NotificationCompat.Action snoozeAction =
new NotificationCompat.Action.Builder(
R.drawable.ic_alarm_white_48dp,
"Snooze",
snoozePendingIntent)
.build();
// Dismiss Action.
Intent dismissIntent = new Intent(this, BigTextIntentService.class);
dismissIntent.setAction(BigTextIntentService.ACTION_DISMISS);
PendingIntent dismissPendingIntent = PendingIntent.getService(this, 0, dismissIntent, 0);
NotificationCompat.Action dismissAction =
new NotificationCompat.Action.Builder(
R.drawable.ic_cancel_white_48dp,
"Dismiss",
dismissPendingIntent)
.build();
// 5. Build and issue the notification.
// Because we want this to be a new notification (not updating a previous notification), we
// create a new Builder. Later, we use the same global builder to get back the notification
// we built here for the snooze action, that is, canceling the notification and relaunching
// it several seconds later.
// Notification Channel Id is ignored for Android pre O (26).
NotificationCompat.Builder notificationCompatBuilder =
new NotificationCompat.Builder(
getApplicationContext(), notificationChannelId);
GlobalNotificationBuilder.setNotificationCompatBuilderInstance(notificationCompatBuilder);
Notification notification = notificationCompatBuilder
// BIG_TEXT_STYLE sets title and content for API 16 (4.1 and after).
.setStyle(bigTextStyle)
// Title for API <16 (4.0 and below) devices.
.setContentTitle(bigTextStyleReminderAppData.getContentTitle())
// Content for API <24 (7.0 and below) devices.
.setContentText(bigTextStyleReminderAppData.getContentText())
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(
getResources(),
R.drawable.ic_alarm_white_48dp))
.setContentIntent(notifyPendingIntent)
// Set primary color (important for Wear 2.0 Notifications).
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
// SIDE NOTE: Auto-bundling is enabled for 4 or more notifications on API 24+ (N+)
// devices and all Android Wear devices. If you have more than one notification and
// you prefer a different summary notification, set a group key and create a
// summary notification via
// .setGroupSummary(true)
// .setGroup(GROUP_KEY_YOUR_NAME_HERE)
.setCategory(Notification.CATEGORY_REMINDER)
// Sets priority for 25 and below. For 26 and above, 'priority' is deprecated for
// 'importance' which is set in the NotificationChannel. The integers representing
// 'priority' are different from 'importance', so make sure you don't mix them.
.setPriority(bigTextStyleReminderAppData.getPriority())
// Sets lock-screen visibility for 25 and below. For 26 and above, lock screen
// visibility is set in the NotificationChannel.
.setVisibility(bigTextStyleReminderAppData.getChannelLockscreenVisibility())
// Adds additional actions specified above.
.addAction(snoozeAction)
.addAction(dismissAction)
.build();
mNotificationManagerCompat.notify(NOTIFICATION_ID, notification);
}
/*
* Generates a BIG_PICTURE_STYLE Notification that supports both phone/tablet and wear. For
* devices on API level 16 (4.1.x - Jelly Bean) and after, displays BIG_PICTURE_STYLE.
* Otherwise, displays a basic notification.
*
* This example Notification is a social post. It allows updating the notification with
* comments/responses via RemoteInput and the BigPictureSocialIntentService on 24+ (N+) and
* Android Wear devices.
*/
private void generateBigPictureStyleNotification() {
Log.d(TAG, "generateBigPictureStyleNotification()");
// Main steps for building a BIG_PICTURE_STYLE notification:
// 0. Get your data
// 1. Create/Retrieve Notification Channel for O and beyond devices (26+)
// 2. Build the BIG_PICTURE_STYLE
// 3. Set up main Intent for notification
// 4. Set up RemoteInput, so users can input (keyboard and voice) from notification
// 5. Build and issue the notification
// 0. Get your data (everything unique per Notification).
MockDatabase.BigPictureStyleSocialAppData bigPictureStyleSocialAppData =
MockDatabase.getBigPictureStyleData();
// 1. Create/Retrieve Notification Channel for O and beyond devices (26+).
String notificationChannelId =
NotificationUtil.createNotificationChannel(this, bigPictureStyleSocialAppData);
// 2. Build the BIG_PICTURE_STYLE.
BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle()
// Provides the bitmap for the BigPicture notification.
.bigPicture(
BitmapFactory.decodeResource(
getResources(),
bigPictureStyleSocialAppData.getBigImage()))
// Overrides ContentTitle in the big form of the template.
.setBigContentTitle(bigPictureStyleSocialAppData.getBigContentTitle())
// Summary line after the detail section in the big form of the template.
.setSummaryText(bigPictureStyleSocialAppData.getSummaryText());
// 3. Set up main Intent for notification.
Intent mainIntent = new Intent(this, BigPictureSocialMainActivity.class);
// When creating your Intent, you need to take into account the back state, i.e., what
// happens after your Activity launches and the user presses the back button.
// There are two options:
// 1. Regular activity - You're starting an Activity that's part of the application's
// normal workflow.
// 2. Special activity - The user only sees this Activity if it's started from a
// notification. In a sense, the Activity extends the notification by providing
// information that would be hard to display in the notification itself.
// Even though this sample's MainActivity doesn't link to the Activity this Notification
// launches directly, i.e., it isn't part of the normal workflow, a social app generally
// always links to individual posts as part of the app flow, so we will follow option 1.
// For an example of option 2, check out the BIG_TEXT_STYLE example.
// For more information, check out our dev article:
// https://developer.android.com/training/notify-user/navigation.html
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack.
stackBuilder.addParentStack(BigPictureSocialMainActivity.class);
// Adds the Intent to the top of the stack.
stackBuilder.addNextIntent(mainIntent);
// Gets a PendingIntent containing the entire back stack.
PendingIntent mainPendingIntent =
PendingIntent.getActivity(
this,
0,
mainIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
// 4. Set up RemoteInput, so users can input (keyboard and voice) from notification.
// Note: For API <24 (M and below) we need to use an Activity, so the lock-screen presents
// the auth challenge. For API 24+ (N and above), we use a Service (could be a
// BroadcastReceiver), so the user can input from Notification or lock-screen (they have
// choice to allow) without leaving the notification.
// Create the RemoteInput.
String replyLabel = getString(R.string.reply_label);
RemoteInput remoteInput =
new RemoteInput.Builder(BigPictureSocialIntentService.EXTRA_COMMENT)
.setLabel(replyLabel)
// List of quick response choices for any wearables paired with the phone
.setChoices(bigPictureStyleSocialAppData.getPossiblePostResponses())
.build();
// Pending intent =
// API <24 (M and below): activity so the lock-screen presents the auth challenge
// API 24+ (N and above): this should be a Service or BroadcastReceiver
PendingIntent replyActionPendingIntent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Intent intent = new Intent(this, BigPictureSocialIntentService.class);
intent.setAction(BigPictureSocialIntentService.ACTION_COMMENT);
replyActionPendingIntent = PendingIntent.getService(this, 0, intent, 0);
} else {
replyActionPendingIntent = mainPendingIntent;
}
NotificationCompat.Action replyAction =
new NotificationCompat.Action.Builder(
R.drawable.ic_reply_white_18dp,
replyLabel,
replyActionPendingIntent)
.addRemoteInput(remoteInput)
.build();
// 5. Build and issue the notification.
// Because we want this to be a new notification (not updating a previous notification), we
// create a new Builder. Later, we use the same global builder to get back the notification
// we built here for a comment on the post.
NotificationCompat.Builder notificationCompatBuilder =
new NotificationCompat.Builder(getApplicationContext(), notificationChannelId);
GlobalNotificationBuilder.setNotificationCompatBuilderInstance(notificationCompatBuilder);
notificationCompatBuilder
// BIG_PICTURE_STYLE sets title and content for API 16 (4.1 and after).
.setStyle(bigPictureStyle)
// Title for API <16 (4.0 and below) devices.
.setContentTitle(bigPictureStyleSocialAppData.getContentTitle())
// Content for API <24 (7.0 and below) devices.
.setContentText(bigPictureStyleSocialAppData.getContentText())
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(
getResources(),
R.drawable.ic_person_black_48dp))
.setContentIntent(mainPendingIntent)
// Set primary color (important for Wear 2.0 Notifications).
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
// SIDE NOTE: Auto-bundling is enabled for 4 or more notifications on API 24+ (N+)
// devices and all Android Wear devices. If you have more than one notification and
// you prefer a different summary notification, set a group key and create a
// summary notification via
// .setGroupSummary(true)
// .setGroup(GROUP_KEY_YOUR_NAME_HERE)
.setSubText(Integer.toString(1))
.addAction(replyAction)
.setCategory(Notification.CATEGORY_SOCIAL)
// Sets priority for 25 and below. For 26 and above, 'priority' is deprecated for
// 'importance' which is set in the NotificationChannel. The integers representing
// 'priority' are different from 'importance', so make sure you don't mix them.
.setPriority(bigPictureStyleSocialAppData.getPriority())
// Sets lock-screen visibility for 25 and below. For 26 and above, lock screen
// visibility is set in the NotificationChannel.
.setVisibility(bigPictureStyleSocialAppData.getChannelLockscreenVisibility());
// If the phone is in "Do not disturb mode, the user will still be notified if
// the sender(s) is starred as a favorite.
for (String name : bigPictureStyleSocialAppData.getParticipants()) {
notificationCompatBuilder.addPerson(name);
}
Notification notification = notificationCompatBuilder.build();
mNotificationManagerCompat.notify(NOTIFICATION_ID, notification);
}
/*
* Generates a INBOX_STYLE Notification that supports both phone/tablet and wear. For devices
* on API level 16 (4.1.x - Jelly Bean) and after, displays INBOX_STYLE. Otherwise, displays a
* basic notification.
*/
private void generateInboxStyleNotification() {
Log.d(TAG, "generateInboxStyleNotification()");
// Main steps for building a INBOX_STYLE notification:
// 0. Get your data
// 1. Create/Retrieve Notification Channel for O and beyond devices (26+)
// 2. Build the INBOX_STYLE
// 3. Set up main Intent for notification
// 4. Build and issue the notification
// 0. Get your data (everything unique per Notification).
MockDatabase.InboxStyleEmailAppData inboxStyleEmailAppData =
MockDatabase.getInboxStyleData();
// 1. Create/Retrieve Notification Channel for O and beyond devices (26+).
String notificationChannelId =
NotificationUtil.createNotificationChannel(this, inboxStyleEmailAppData);
// 2. Build the INBOX_STYLE.
InboxStyle inboxStyle = new NotificationCompat.InboxStyle()
// This title is slightly different than regular title, since I know INBOX_STYLE is
// available.
.setBigContentTitle(inboxStyleEmailAppData.getBigContentTitle())
.setSummaryText(inboxStyleEmailAppData.getSummaryText());
// Add each summary line of the new emails, you can add up to 5.
for (String summary : inboxStyleEmailAppData.getIndividualEmailSummary()) {
inboxStyle.addLine(summary);
}
// 3. Set up main Intent for notification.
Intent mainIntent = new Intent(this, InboxMainActivity.class);
// When creating your Intent, you need to take into account the back state, i.e., what
// happens after your Activity launches and the user presses the back button.
// There are two options:
// 1. Regular activity - You're starting an Activity that's part of the application's
// normal workflow.
// 2. Special activity - The user only sees this Activity if it's started from a
// notification. In a sense, the Activity extends the notification by providing
// information that would be hard to display in the notification itself.
// Even though this sample's MainActivity doesn't link to the Activity this Notification
// launches directly, i.e., it isn't part of the normal workflow, a eamil app generally
// always links to individual emails as part of the app flow, so we will follow option 1.
// For an example of option 2, check out the BIG_TEXT_STYLE example.
// For more information, check out our dev article:
// https://developer.android.com/training/notify-user/navigation.html
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack.
stackBuilder.addParentStack(InboxMainActivity.class);
// Adds the Intent to the top of the stack.
stackBuilder.addNextIntent(mainIntent);
// Gets a PendingIntent containing the entire back stack.
PendingIntent mainPendingIntent =
PendingIntent.getActivity(
this,
0,
mainIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
// 4. Build and issue the notification.
// Because we want this to be a new notification (not updating a previous notification), we
// create a new Builder. However, we don't need to update this notification later, so we
// will not need to set a global builder for access to the notification later.
NotificationCompat.Builder notificationCompatBuilder =
new NotificationCompat.Builder(getApplicationContext(), notificationChannelId);
GlobalNotificationBuilder.setNotificationCompatBuilderInstance(notificationCompatBuilder);
notificationCompatBuilder
// INBOX_STYLE sets title and content for API 16+ (4.1 and after) when the
// notification is expanded.
.setStyle(inboxStyle)
// Title for API <16 (4.0 and below) devices and API 16+ (4.1 and after) when the
// notification is collapsed.
.setContentTitle(inboxStyleEmailAppData.getContentTitle())
// Content for API <24 (7.0 and below) devices and API 16+ (4.1 and after) when the
// notification is collapsed.
.setContentText(inboxStyleEmailAppData.getContentText())
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(
getResources(),
R.drawable.ic_person_black_48dp))
.setContentIntent(mainPendingIntent)
// Set primary color (important for Wear 2.0 Notifications).
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
// SIDE NOTE: Auto-bundling is enabled for 4 or more notifications on API 24+ (N+)
// devices and all Android Wear devices. If you have more than one notification and
// you prefer a different summary notification, set a group key and create a
// summary notification via
// .setGroupSummary(true)
// .setGroup(GROUP_KEY_YOUR_NAME_HERE)
// Sets large number at the right-hand side of the notification for API <24 devices.
.setSubText(Integer.toString(inboxStyleEmailAppData.getNumberOfNewEmails()))
.setCategory(Notification.CATEGORY_EMAIL)
// Sets priority for 25 and below. For 26 and above, 'priority' is deprecated for
// 'importance' which is set in the NotificationChannel. The integers representing
// 'priority' are different from 'importance', so make sure you don't mix them.
.setPriority(inboxStyleEmailAppData.getPriority())
// Sets lock-screen visibility for 25 and below. For 26 and above, lock screen
// visibility is set in the NotificationChannel.
.setVisibility(inboxStyleEmailAppData.getChannelLockscreenVisibility());
// If the phone is in "Do not disturb mode, the user will still be notified if
// the sender(s) is starred as a favorite.
for (String name : inboxStyleEmailAppData.getParticipants()) {
notificationCompatBuilder.addPerson(name);
}
Notification notification = notificationCompatBuilder.build();
mNotificationManagerCompat.notify(NOTIFICATION_ID, notification);
}
/*
* Generates a MESSAGING_STYLE Notification that supports both phone/tablet and wear. For
* devices on API level 24 (7.0 - Nougat) and after, displays MESSAGING_STYLE. Otherwise,
* displays a basic BIG_TEXT_STYLE.
*/
private void generateMessagingStyleNotification() {
Log.d(TAG, "generateMessagingStyleNotification()");
// Main steps for building a MESSAGING_STYLE notification:
// 0. Get your data
// 1. Create/Retrieve Notification Channel for O and beyond devices (26+)
// 2. Build the MESSAGING_STYLE
// 3. Set up main Intent for notification
// 4. Set up RemoteInput (users can input directly from notification)
// 5. Build and issue the notification
// 0. Get your data (everything unique per Notification)
MockDatabase.MessagingStyleCommsAppData messagingStyleCommsAppData =
MockDatabase.getMessagingStyleData();
// 1. Create/Retrieve Notification Channel for O and beyond devices (26+).
String notificationChannelId =
NotificationUtil.createNotificationChannel(this, messagingStyleCommsAppData);
// 2. Build the Notification.Style (MESSAGING_STYLE).
String contentTitle = messagingStyleCommsAppData.getContentTitle();
MessagingStyle messagingStyle =
new NotificationCompat.MessagingStyle(messagingStyleCommsAppData.getReplayName())
// This could be the user-created name of the group or, if it doesn't have
// a specific name, a list of the participants in the conversation. Do not
// set a conversation title for one-on-one chats, since platforms use the
// existence of this field as a hint that the conversation is a group.
//
// In our case, we use the same title.
.setConversationTitle(contentTitle);
// Adds all Messages.
// Note: Messages include the text, timestamp, and sender.
for (MessagingStyle.Message message : messagingStyleCommsAppData.getMessages()) {
messagingStyle.addMessage(message);
}
// 3. Set up main Intent for notification.
Intent notifyIntent = new Intent(this, MessagingMainActivity.class);
// When creating your Intent, you need to take into account the back state, i.e., what
// happens after your Activity launches and the user presses the back button.
// There are two options:
// 1. Regular activity - You're starting an Activity that's part of the application's
// normal workflow.
// 2. Special activity - The user only sees this Activity if it's started from a
// notification. In a sense, the Activity extends the notification by providing
// information that would be hard to display in the notification itself.
// Even though this sample's MainActivity doesn't link to the Activity this Notification
// launches directly, i.e., it isn't part of the normal workflow, a chat app generally
// always links to individual conversations as part of the app flow, so we will follow
// option 1.
// For an example of option 2, check out the BIG_TEXT_STYLE example.
// For more information, check out our dev article:
// https://developer.android.com/training/notify-user/navigation.html
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack
stackBuilder.addParentStack(MessagingMainActivity.class);
// Adds the Intent to the top of the stack
stackBuilder.addNextIntent(notifyIntent);
// Gets a PendingIntent containing the entire back stack
PendingIntent mainPendingIntent =
PendingIntent.getActivity(
this,
0,
notifyIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
// 4. Set up RemoteInput, so users can input (keyboard and voice) from notification.
// Note: For API <24 (M and below) we need to use an Activity, so the lock-screen present
// the auth challenge. For API 24+ (N and above), we use a Service (could be a
// BroadcastReceiver), so the user can input from Notification or lock-screen (they have
// choice to allow) without leaving the notification.
// Create the RemoteInput specifying this key.
String replyLabel = getString(R.string.reply_label);
RemoteInput remoteInput = new RemoteInput.Builder(MessagingIntentService.EXTRA_REPLY)
.setLabel(replyLabel)
.build();
// Pending intent =
// API <24 (M and below): activity so the lock-screen presents the auth challenge.
// API 24+ (N and above): this should be a Service or BroadcastReceiver.
PendingIntent replyActionPendingIntent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Intent intent = new Intent(this, MessagingIntentService.class);
intent.setAction(MessagingIntentService.ACTION_REPLY);
replyActionPendingIntent = PendingIntent.getService(this, 0, intent, 0);
} else {
replyActionPendingIntent = mainPendingIntent;
}
NotificationCompat.Action replyAction =
new NotificationCompat.Action.Builder(
R.drawable.ic_reply_white_18dp,
replyLabel,
replyActionPendingIntent)
.addRemoteInput(remoteInput)
// Allows system to generate replies by context of conversation.
.setAllowGeneratedReplies(true)
.build();
// 5. Build and issue the notification.
// Because we want this to be a new notification (not updating current notification), we
// create a new Builder. Later, we update this same notification, so we need to save this
// Builder globally (as outlined earlier).
NotificationCompat.Builder notificationCompatBuilder =
new NotificationCompat.Builder(getApplicationContext(), notificationChannelId);
GlobalNotificationBuilder.setNotificationCompatBuilderInstance(notificationCompatBuilder);
notificationCompatBuilder
// MESSAGING_STYLE sets title and content for API 16 and above devices.
.setStyle(messagingStyle)
// Title for API < 16 devices.
.setContentTitle(contentTitle)
// Content for API < 16 devices.
.setContentText(messagingStyleCommsAppData.getContentText())
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(
getResources(),
R.drawable.ic_person_black_48dp))
.setContentIntent(mainPendingIntent)
// Set primary color (important for Wear 2.0 Notifications).
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
// SIDE NOTE: Auto-bundling is enabled for 4 or more notifications on API 24+ (N+)
// devices and all Android Wear devices. If you have more than one notification and
// you prefer a different summary notification, set a group key and create a
// summary notification via
// .setGroupSummary(true)
// .setGroup(GROUP_KEY_YOUR_NAME_HERE)
// Number of new notifications for API <24 (M and below) devices.
.setSubText(Integer.toString(messagingStyleCommsAppData.getNumberOfNewMessages()))
.addAction(replyAction)
.setCategory(Notification.CATEGORY_MESSAGE)
// Sets priority for 25 and below. For 26 and above, 'priority' is deprecated for
// 'importance' which is set in the NotificationChannel. The integers representing
// 'priority' are different from 'importance', so make sure you don't mix them.
.setPriority(messagingStyleCommsAppData.getPriority())
// Sets lock-screen visibility for 25 and below. For 26 and above, lock screen
// visibility is set in the NotificationChannel.
.setVisibility(messagingStyleCommsAppData.getChannelLockscreenVisibility());
// If the phone is in "Do not disturb mode, the user will still be notified if
// the sender(s) is starred as a favorite.
for (String name : messagingStyleCommsAppData.getParticipants()) {
notificationCompatBuilder.addPerson(name);
}
Notification notification = notificationCompatBuilder.build();
mNotificationManagerCompat.notify(NOTIFICATION_ID, notification);
}
/**
* Helper method for the SnackBar action, i.e., if the user has this application's notifications
* disabled, this opens up the dialog to turn them back on after the user requests a
* Notification launch.
*
* IMPORTANT NOTE: You should not do this action unless the user takes an action to see your
* Notifications like this sample demonstrates. Spamming users to re-enable your notifications
* is a bad idea.
*/
private void openNotificationSettingsForApp() {
// Links to this app's notification settings.
Intent intent = new Intent();
intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
intent.putExtra("app_package", getPackageName());
intent.putExtra("app_uid", getApplicationInfo().uid);
startActivity(intent);
}
}