1 /* 2 * Copyright 2017 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.example.android.wearable.wear.messaging.util; 17 18 import android.app.AlarmManager; 19 import android.app.PendingIntent; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.os.SystemClock; 23 import android.util.Log; 24 import com.example.android.wearable.wear.messaging.chat.MockIncomingMessageReceiver; 25 import com.example.android.wearable.wear.messaging.model.Chat; 26 import com.example.android.wearable.wear.messaging.model.Message; 27 import java.util.concurrent.TimeUnit; 28 29 /** 30 * Manage an alarm manager to trigger a notification after 5 seconds. 31 * 32 * <p>Demonstrates the receiving of a notification. In a real app, you would want to use FCM to 33 * handle pushing notifications to a device. 34 */ 35 public class SchedulerHelper { 36 37 private static final String TAG = "SchedulerHelper"; 38 39 public static void scheduleMockNotification(Context context, Chat chat, Message message) { 40 AlarmManager alarmManger = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 41 PendingIntent alarmIntent = createPendingIntentToNotifyMessage(context, chat, message); 42 43 Log.d(TAG, "Setting up alarm to be triggered shortly."); 44 alarmManger.set( 45 AlarmManager.ELAPSED_REALTIME_WAKEUP, 46 SystemClock.elapsedRealtime() + TimeUnit.SECONDS.toMillis(5), 47 alarmIntent); 48 } 49 50 private static PendingIntent createPendingIntentToNotifyMessage( 51 Context context, Chat chat, Message message) { 52 Intent intent = new Intent(context, MockIncomingMessageReceiver.class); 53 intent.setAction(Constants.ACTION_RECEIVE_MESSAGE); 54 intent.putExtra(Constants.EXTRA_CHAT, chat.getId()); 55 intent.putExtra(Constants.EXTRA_MESSAGE, message.getId()); 56 57 return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); 58 } 59 } 60