Android Notifications (Part 2) Plus Alarms and BroadcastReceivers

Slides:



Advertisements
Similar presentations
Android Application Development Tutorial. Topics Lecture 6 Overview Programming Tutorial 3: Sending/Receiving SMS Messages.
Advertisements

Cosc 5/4730 Android Services. What is a service? From android developer web pages: Most confusion about the Service class actually revolves around what.
Cosc 4755 Android Notifications. There are a couple of ways to notify users with interrupting what they are doing The first is Toast, use the factory.
Notifications & Alarms.  Notifications  Alarms.
1 Working with the Android Services Nilanjan Banerjee Mobile Systems Programming University of Arkansas Fayetteville, AR
Cosc 5/4730 Android SMS. A note first Depending on the API level, an import changes because of a deprecated API 3 uses – import android.telephony.gsm.SmsManager;
System broadcasts and services. System broadcast events. EventDescription Intent.ACTION_BOOT_COMPLETEDBoot completed. Requires the android.permission.RECE.
SMS. Short Message Service – Primarily text messages between mobile phones – First one sent December 3, 1982 “Merry Christmas” – In 2008 Approximately.
Intent An Intent describes the operation to be performed. Intents are used to start an activity using either of the following methods – Context.startActivity()
Developing Push Notifications (C2DM) for Android Vijai Co-Founder Adhish Technologies, Sweet’N’Spicy apps.
Mobile Computing Lecture#08 IntentFilters & BroadcastReceivers.
Favorite Twitter® Searches App Android How to Program © by Pearson Education, Inc. All Rights Reserved.
Google Cloud Messaging for Android (GCM) is a free service that helps developers send data from servers to their Android.
Broadcast Receiver Android Club Agenda Broadcast Receiver Widget.
8. Notification과 Alarm.
Cosc 5/4730 Introduction: Threads, Android Activities, and MVC.
Homescreen Widgets Demystified Pearl AndroidTO // Oct 26, 2010.
Mobile Programming Lecture 17 Creating Homescreen Widgets.
Cosc 5/4730 Android Content Providers and Intents.
Using Intents to Broadcast Events Intents Can be used to broadcast messages anonymously Between components via the sendBroadcast method As a result Broadcast.
Cosc 5/4730 Broadcast Receiver. Broadcast receiver A broadcast receiver (short receiver) – is an Android component which allows you to register for system.
Android ICC Part II Inter-component communication.
Mobile Programming Lecture 6
DUE Hello World on the Android Platform.
16 Services and Broadcast Receivers CSNB544 Mobile Application Development Thanks to Utexas Austin.
1 Announcements Homework #2 due Feb 7 at 1:30pm Submit the entire Eclipse project in Blackboard Please fill out the when2meets when your Project Manager.
Threads and Services. Background Processes One of the key differences between Android and iPhone is the ability to run things in the background on Android.
Cosc 5/4730 Android App Widgets. App Widgets App Widgets are miniature application views that can be embedded in other applications (such as the Home.
Cosc 5/4730 Android Communications Intents, callbacks, and setters.
User notification Android Club Agenda Toast Custom Toast Notification Dialog.
Introducing Intents Intents Bind application components and navigate between them Transform device into collection of interconnected systems Creating a.
Cosc 4735 Primer: Marshmallow Changes and new APIs in android 6.0 (api 23)
Services Background operating component without a visual interface Running in the background indefinitely Differently from Activity, Service in Android.
Lecture 2: Android Concepts
Cosc 5/4735 Voice Actions Voice Interactions (API 23+)
Speech Service & client(Activity) 오지영.
Intents and Broadcast Receivers Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution.
Mobile Software Development for Android - I397 IT COLLEGE, ANDRES KÄVER, WEB:
David Sutton SMS TELEPHONY IN ANDROID. OUTLINE  This week’s exercise, an SMS Pub Quiz  Simulating telephony on an emulator  Broadcast Intents and broadcast.
Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Putting together the AndroidManifest.xml file Creating multiple.
Messaging and Networking. Objectives Send SMS messages using the Messaging app Send SMS messages programmatically from within your app Receive and consume.
Cosc 4735 Nougat API 24+ additions.
Lab7 – Appendix.
Intents and Broadcast Receivers
Mobile Software Development for Android - I397
Lecture 2: Android Concepts
Android Application Development 1 6 May 2018
CS499 – Mobile Application Development
Broadcast receivers.
CS371m - Mobile Computing Services and Broadcast Receivers
Messaging Unit-4.
Activities and Intents
The Android Activity Lifecycle
Broadcast Receivers A Android Component where you can register for system or application events, receive and react to broadcast intent. Each broadcast.
Mobile Software Development for Android - I397
Firebase Cloud messaging A primer
Developing Android Services
Android Notifications (Part 1)
Application Fundamentals
CIS 470 Mobile App Development
Activities and Intents
CIS 470 Mobile App Development
Android Notifications
Android Developer Fundamentals V2 Lesson 5
Mobile Programming Dr. Mohsin Ali Memon.
Notifying from the Background
Application Fundamentals
Lecture 2: Android Concepts
Activities, Fragments, and Intents
Mobile Programming Broadcast Receivers.
Presentation transcript:

Android Notifications (Part 2) Plus Alarms and BroadcastReceivers Cosc 4730 Android Notifications (Part 2) Plus Alarms and BroadcastReceivers

Notifications add on Android 7.0 Notifications have added compatibility with android auto and wear. Allows for remoteinput (reply) Has the notification been read Has the notification been deleted How many notifications are there.

Android Auto Add this to your androidmanifest.xml inside the between <application> tags, before the <activity> <meta-data android:name= "com.google.android.gms.car.application" android:resource="@xml/automotive_app_desc"/> Where the xml file has the following lines: <?xml version="1.0" encoding="utf-8"?> <automotiveApp> <uses name="notification"/> </automotiveApp> Then Ensure that Message notifications are extended using NotificationCompat.Builder.extend(new CarExtender()...) This is just part of the notification build.

Notifying the app about notifications. For read and delete Basically the same idea Create an pentingintent Put the message id in the intent. Have a broadcast receiver The pentingintent will be sent to the receiver and you know which notification it was, based on the id number.

Read example The Pendingintent PendingIntent.getBroadcast(getActivity().getApplicationContext(), NotificationNum, new Intent() .addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES) .setAction("edu.cs4730.notification3.ACTION_MESSAGE_READ") .putExtra("conversation_id", NotificationNum), PendingIntent.FLAG_UPDATE_CURRENT); Then as the notification is being build .setContentIntent(readPendingIntent) Delete uses .setDeleteIntent(DeletePendingIntent):

Read example (2) Either declare a broadcast receiver static or dynamic private BroadcastReceiver mReadReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Log.d(TAG, "onReceiveRead"); int conversationId = intent.getIntExtra(CONVERSATION_ID, -1); if (conversationId != -1) { //now you know, do what? } } }; In onResume registerReceiver(mReadReceiver, new IntentFilter("edu.cs4730.notification3.ACTION_MESSAGE_READ"));

remoteInput basically the same idea, but far more complex Build a RemoteInput for receiving voice input in a Car Notification or text input on devices that support text input (like devices on Android N and above). Building a Pending Intent for the reply action to trigger Build an Android N compatible Remote Input enabled action. Create the UnreadConversation builderand populate it with the participant name, read and reply intents. Then in the notification builder, add it with the extend (and carextender() for compatilibity). This is best all seen in the example code, instead of here.

remoteInput (2) Again, we need broadcast receiver for the reply and the intentfilter (like read) In the receiver we can get the reply message from the remoteinput intent and the message id Finally, we need to update the notification that we have received it, so we build a simple notification, using the same id number. Note, for android 8.X, if you are using sound/vibrate, use .setOnlyAlertOnce(true) in the update.

Example notificationDemo3 Show how to setup a read, delete, and reply for notifications. You may need to review notifications to under some of what is going on.

References for android 7.x notifications https://developer.android.com/guide/topics/ui/notifiers/notifications.html https://developer.android.com/about/versions/nougat/android-7.0.html#notification_enhancements https://github.com/googlesamples/android-MessagingService https://github.com/googlesamples/android-ActiveNotifications/

But I want to notify a user later! There is no native way to that with the notifications. You need to use an AlarmManager and calendar object. And a broadcast receiver. Which we will cover first and then come back to it.

Calendar

Calendar object It a pretty simple object, Used briefly in the GUI demo for the date/time pickers. Get an intance, which has the current time set. Calendar calendar = Calendar.getInstance(); Use a get(..) and set(..) to change the information as needed. Set using the following constants Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH, Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND Say I want to set the calendar to 4:40am calendar.set(Calendar.HOUR_OF_DAY, 4); calendar.set(Calendar.MINUTE, 40); calendar.set(Calendar.SECOND, 0); As note, if it is already passed 4:40am, there would be a problem later Say I want to set it for 2 minutes from now. calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) +2);

AlarmManager

AlarmManager These allow you to schedule your application to be run at some point in the future. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. We get an in AlarmManager service Set an intent and pendingIntent for the activity (or say a broadcast receiver) to call. A time in Milliseconds when to send the intent And that we want a “wakeup call” at that time. Get the AlarmManager AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

Example code Using the calendar object for 2 minutes later. Create the intent and pendingIntent Intent notificationIntent = new Intent("edu.cs4730.notificationdemo.DisplayNotification"); PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this, NotID, notificationIntent, 0); Now set the alarm alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), contentIntent);

Example code (2) What is this: Intent("edu.cs4730.notificationdemo.DisplayNotification"); We could also just give a Activity name. It comes from the manifest file where we describe the activity or broadcast receiver. This case an activity. <activity android:name=".DisplayNotification" … /> <intent-filter> <action android:name="edu.cs4730.notificationdemo.DisplayNotification" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>

Finally! The displayNotification activity is called when the alarm goes off. And it’s the displayNotification activity that now creates and notification.

Broadcast Receiver

Example public class myBroadcastReciever extends BroadcastReceiver { private static final String ACTION = "edu.cs4730.notificationdemo.broadNotification"; @Override public void onReceive(Context context, Intent intent) { String info= "no bundle"; if (intent.getAction().equals(ACTION)){ //is it our action Bundle extras = intent.getExtras(); if (extras != null) { info = extras.getString("mytype"); if (info == null) { info = "nothing"; } Likely toast or start an activity or notification. Whatever is needed now. }}}

Demo code notificationDemo project All the different notifications listed in here Lots of buttons to try out each one. Alarms and the broadcastReceiver notificationDemo2 project Need first one installed Sends to the broadcast receiver And uses an alarm to send to the broadcast receiver notificiationDemo3.zip Android 7.0 notificaitons. You may also find http://developer.android.com/guide/topics/ui/notifiers/notifications.html helpful.

References http://developer.android.com/reference/android/app/Notification.html http://developer.android.com/reference/android/content/BroadcastReceiver.html http://stackoverflow.com/questions/12372654/how-to-trigger-broadcast-receiver-from-notification http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html https://developer.android.com/guide/topics/ui/notifiers/notifications.html

Q A &