This project will have you create a page of buttons to display different types of notifications when pressed.
- Create a new empty activity
- Add a button called "Get Notification"
- Use
context.getSystemService(Context.NOTIFICATION_SERVICE)
to get a handle on theNotificationManager
- Create a listener for the button.
- Check the
Build.VERSION.SDK_INT
if it is greater than or equal toBuild.VERSION_CODES.O
, build a channel usingnew NotificationChannel(channelId, name, importance)
- Add a description using
channel.setDescription
- Register the channel using
notificationManager.createNotificationChannel(channel)
- Create a
NotificationCompat.Builder
using theActivity
's context and the channel id you just used. - Set the
Priority
,ContentTitle
,ContentText
,SmallIcon
,Color
, andDefaults
- Use the
notificationManager.notify
method withNOTIFICATION_ID_INSTANT
andbuilder.build()
to execute the notification - Compile and test the app to make sure everything works.
- Change the notification pritory to see the difference it makes, explore changes in both the notification builder and notification channel
- Build and test with each of these with various combinations
- Create a new Fullscreen Activity
- Remove the bottom frame and its contents
Be sure to remove all references to these views in the FulscreenActivity java file as well
- In the
onCreate
method of the new activity, get the intent and pull a string extra from it. We will be sending that string extra to this activity from our notification - Use the string you just got to set the text of the activity's
mContentView
You'll have to change its data type from
View
toTextView
before doing so
- Go back to your button's onclick listener and create an intent to launch your new activity and add a string extra to it.
Your string should let you know how the intent was launched in this case it could be "Noticiation Tapped" or "Content Intent"
-
Instead of calling
startActivity
with this intent, we're going to wrap it with aPendingIntent
object a. Call the static methodPendingIntent.getActivity
b. pass it yourcontext
, a request code of 0, the intent you created, andPendingIntent.FLAG_ONE_SHOT
Thte ONE_SHOT means the intent can only be used once, but since you have AutoCancel set to true, the notification will be destroyed once you execute the intent anyway
-
In your notification builder, set the
ContentIntent
with your newPendingIntent
-
Build and test your app to make sure it launches the new activity and displays your string
- Create another
PendingIntent
with a different string extra.
The string extra in this intent must use the same key as the one for your other intent as your FullscreenActivity won't differentiate. Also be sure that this
PendingIntent
has a differentRequest Code
than your other intent
- Add an
Action
to your notification builder. Pass it a drawable id for an icon, a title, and your pending intent - Build and test your app to make sure it launches the new activity and displays your string.
In your testing, find bugs and small feature improvements that can improve the polish of your app.