Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MessagingIntentService extends IntentService {
- private static final String TAG = "MessagingIntentService";
- public static final String ACTION_REPLY =
- "com.example.android.wearable.wear.wearnotifications.handlers.action.REPLY";
- public static final String EXTRA_REPLY =
- "com.example.android.wearable.wear.wearnotifications.handlers.extra.REPLY";
- public MessagingIntentService() {
- super("MessagingIntentService");
- }
- @Override
- protected void onHandleIntent(Intent intent) {
- Log.d(TAG, "onHandleIntent(): " + intent);
- if (intent != null) {
- final String action = intent.getAction();
- if (ACTION_REPLY.equals(action)) {
- handleActionReply(getMessage(intent));
- }
- }
- }
- private void handleActionReply(CharSequence replyCharSequence) {
- Log.d(TAG, "handleActionReply(): " + replyCharSequence);
- if (replyCharSequence != null) {
- NotificationCompat.Builder notificationCompatBuilder =
- GlobalNotificationBuilder.getNotificationCompatBuilderInstance();
- if (notificationCompatBuilder == null) {
- notificationCompatBuilder = recreateBuilderWithMessagingStyle();
- }
- Notification notification = notificationCompatBuilder.build();
- MessagingStyle messagingStyle =
- NotificationCompat.MessagingStyle.extractMessagingStyleFromNotification(
- notification);
- messagingStyle.addMessage(replyCharSequence, System.currentTimeMillis(), (Person) null);
- notification = notificationCompatBuilder.setStyle(messagingStyle).build();
- NotificationManagerCompat notificationManagerCompat =
- NotificationManagerCompat.from(getApplicationContext());
- notificationManagerCompat.notify(StandaloneMainActivity.NOTIFICATION_ID, notification);
- }
- }
- private CharSequence getMessage(Intent intent) {
- Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
- if (remoteInput != null) {
- return remoteInput.getCharSequence(EXTRA_REPLY);
- }
- return null;
- }
- private NotificationCompat.Builder recreateBuilderWithMessagingStyle() {
- MockDatabase.MessagingStyleCommsAppData messagingStyleCommsAppData =
- MockDatabase.getMessagingStyleData(getApplicationContext());
- String notificationChannelId = messagingStyleCommsAppData.getChannelId();
- String contentTitle = messagingStyleCommsAppData.getContentTitle();
- MessagingStyle messagingStyle =
- new NotificationCompat.MessagingStyle(messagingStyleCommsAppData.getMe())
- .setConversationTitle(contentTitle);
- for (MessagingStyle.Message message : messagingStyleCommsAppData.getMessages()) {
- messagingStyle.addMessage(message);
- }
- messagingStyle.setGroupConversation(messagingStyleCommsAppData.isGroupConversation());
- Intent notifyIntent = new Intent(this, MessagingMainActivity.class);
- PendingIntent mainPendingIntent =
- PendingIntent.getActivity(this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
- String replyLabel = getString(R.string.reply_label);
- RemoteInput remoteInput =
- new RemoteInput.Builder(MessagingIntentService.EXTRA_REPLY)
- .setLabel(replyLabel)
- .setChoices(messagingStyleCommsAppData.getReplyChoicesBasedOnLastMessage())
- .build();
- Intent replyIntent = new Intent(this, MessagingIntentService.class);
- replyIntent.setAction(MessagingIntentService.ACTION_REPLY);
- PendingIntent replyActionPendingIntent = PendingIntent.getService(this, 0, replyIntent, 0);
- final NotificationCompat.Action.WearableExtender inlineActionForWear2 =
- new NotificationCompat.Action.WearableExtender()
- .setHintDisplayActionInline(true)
- .setHintLaunchesActivity(false);
- NotificationCompat.Action replyAction =
- new NotificationCompat.Action.Builder(
- R.drawable.ic_reply_white_18dp,
- replyLabel,
- replyActionPendingIntent)
- .addRemoteInput(remoteInput)
- .setShowsUserInterface(false)
- .setAllowGeneratedReplies(true)
- .setSemanticAction(Action.SEMANTIC_ACTION_REPLY)
- .extend(inlineActionForWear2)
- .build();
- NotificationCompat.Builder notificationCompatBuilder =
- new NotificationCompat.Builder(getApplicationContext(), notificationChannelId);
- GlobalNotificationBuilder.setNotificationCompatBuilderInstance(notificationCompatBuilder);
- notificationCompatBuilder
- .setStyle(messagingStyle)
- .setContentTitle(contentTitle)
- .setContentText(messagingStyleCommsAppData.getContentText())
- .setSmallIcon(R.drawable.ic_launcher)
- .setLargeIcon(
- BitmapFactory.decodeResource(
- getResources(), R.drawable.ic_person_black_48dp))
- .setContentIntent(mainPendingIntent)
- .setDefaults(NotificationCompat.DEFAULT_ALL)
- .setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
- .setSubText(Integer.toString(messagingStyleCommsAppData.getNumberOfNewMessages()))
- .addAction(replyAction)
- .setCategory(Notification.CATEGORY_MESSAGE)
- .setPriority(messagingStyleCommsAppData.getPriority())
- .setVisibility(messagingStyleCommsAppData.getChannelLockscreenVisibility());
- for (Person person : messagingStyleCommsAppData.getParticipants()) {
- notificationCompatBuilder.addPerson(person.getUri());
- }
- return notificationCompatBuilder;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement