Home | History | Annotate | Download | only in support
      1 /*
      2 
      3  * Copyright (C) 2016 The Android Open Source Project
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *      http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 package com.google.android.wearable.support;
     19 
     20 import android.app.PendingIntent;
     21 import android.content.BroadcastReceiver;
     22 import android.content.Context;
     23 import android.content.Intent;
     24 import android.support.v4.app.NotificationCompat;
     25 import android.support.v4.app.NotificationCompat.WearableExtender;
     26 import android.support.v4.app.NotificationManagerCompat;
     27 import android.support.v4.app.RemoteInput;
     28 import android.widget.Toast;
     29 
     30 
     31 /**
     32  * Receiver to generate notification cards.
     33  */
     34 public class CustomPostNotificationReceiver extends BroadcastReceiver {
     35 
     36     public static final String REMOTE_INPUT_KEY = "demoCardQuickReply";
     37     private static final int NOTIFICATION_CARDS_COUNT = 10;
     38 
     39     public CustomPostNotificationReceiver() {
     40     }
     41 
     42     @Override
     43     public void onReceive(Context context, Intent intent) {
     44         String contentTitle = context.getResources().getString(R.string.notification_title);
     45         // Get an instance of NotificationManager service
     46         NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
     47         for (int i = 1; i <= NOTIFICATION_CARDS_COUNT; i++) {
     48             Intent remoteInputIntent = new Intent(context,
     49                     CustomNotificationRemoteInputActivity.class);
     50             PendingIntent remoteInputPendingIntent = PendingIntent
     51                     .getActivity(context, i + NOTIFICATION_CARDS_COUNT, remoteInputIntent,
     52                             PendingIntent.FLAG_UPDATE_CURRENT);
     53             RemoteInput remoteInput = new RemoteInput.Builder(REMOTE_INPUT_KEY)
     54                     .setLabel("Quick reply")
     55                     .build();
     56 
     57             NotificationCompat.Action action = new NotificationCompat.Action.Builder(
     58                     R.drawable.ic_reply, "Reply", remoteInputPendingIntent)
     59                     .addRemoteInput(remoteInput)
     60                     .build();
     61 
     62             NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
     63                     .setSmallIcon(R.drawable.ic_notification)
     64                     .setContentTitle(contentTitle + "-" + i)
     65                     .setCategory(NotificationCompat.CATEGORY_MESSAGE)
     66                     .setContentText(String.format("From iteration %d", i))
     67                     .extend(new WearableExtender()
     68                             .addAction(action));
     69 
     70             // Build the notification and issues it with notification manager
     71             notificationManager.notify(i, notificationBuilder.build());
     72         }
     73 
     74         // Show a toast once notifications cards are posted
     75         Toast.makeText(context, context.getString(R.string.notification_posted), Toast.LENGTH_SHORT)
     76                 .show();
     77     }
     78 }