Reputation: 7347
I have an Android library that has a Service
that creates a Notification
. From what I understand Notification
must have a contentIntent
(PendingIntent
) set or a runtime exception will be thrown.
The problem is that I want users to be able to use this Service
as is, or extend it, so that they can set the PendingIntent
themselves through a callback during the creation of the Notification
. However, if they choose not to do this, I need to set the PendingIntent
to something so that there is no exception. Is there any way to create a dummy PendingIntent
that just acts as a fill-in?
Here's an example of the code from the createNotification
method:
PendingIntent p;
if(getPendingIntentCallback != null) {
p = getPendingIntentCallback.getPendingIntent();
}
else {
p = ?;
}
notification.contentIntent = p;
Upvotes: 3
Views: 1568
Reputation: 81
PendingIntent pi=PendingIntent.getBroadcast(this, 0, new Intent("DoNothing"), 0);
This worked for me. It will launch a broacast for a "DoNothing" action. I hope nobody will listen for a "DoNothing" broadcast and do something as a reaction to it. But if you prefer you can construct something more exotic. Note: I'm using this just as a temporary placeholder. I will substitute it with something more useful for the user as I will advance in my application development.
Upvotes: 8
Reputation: 1628
A notification needs to have some sort of action associated with it. You have to tell android what to do when a app user clicks the notification. Letting your library fail if no contentIntent has been defined will let your library user know that they have missed a very important step.
You could check for the pending intent before creating your notification.
if(getPendingIntentCallback != null) {
p = getPendingIntentCallback.getPendingIntent();
// create a notification
}
else {
//don't create a notification
Log.d("Notification", "Your notification was not created because no pending intent was found");
}
Or to answer the question you asked, you could create a dummy pending intent that performs some arbitrary action like going to the home screen.
See this thread: How to Launch Home Screen Programmatically in Android
Upvotes: 0