Android: NotificationCompat.MediaStyle action buttons don't do anything - android

I've got a simple Android app containing one Activity and a Service that derives from MediaBrowserServiceCompat. I've successfully gotten it set up to play audio from my main activity by using MediaBrowserCompat and MediaControllerCompat. It can even play and pause the audio from my Bluetooth headphones. All good.
My challenge concerns the NotificationCompat.MediaStyle notification that appears on the lock screen and in the notifications tray. The notification appears properly. However, when I add buttons using addAction() and MediaButtonReceiver.buildMediaButtonPendingIntent, they don't do anything. If I instead add a dummy PendingIntent that just launches my main activity, that works fine.
Here's my code to generate the notification (apologies, this is C# running in Xamarin, so the casing and names will be slightly different from what you might expect). This is inside my service class.
var builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.SetVisibility(NotificationCompat.VisibilityPublic)
.SetSmallIcon(Resource.Drawable.ic_launcher)
.SetContentTitle("Title")
.SetContentText("Content")
.SetSubText("Subtext")
.SetLargeIcon(icon)
.SetColor(Android.Graphics.Color.DarkOrange)
.SetContentIntent(intent)
.SetDeleteIntent(MediaButtonReceiver.BuildMediaButtonPendingIntent(this, PlaybackStateCompat.ActionStop))
.AddAction(new NotificationCompat.Action(
Resource.Drawable.ic_pause, "Pause",
MediaButtonReceiver.BuildMediaButtonPendingIntent(this, PlaybackStateCompat.ActionPause)))
.SetStyle(new Android.Support.V4.Media.App.NotificationCompat.MediaStyle()
.SetShowActionsInCompactView(0)
.SetMediaSession(this.mediaSession.SessionToken)
.SetShowCancelButton(true)
.SetCancelButtonIntent(MediaButtonReceiver.BuildMediaButtonPendingIntent(this, PlaybackStateCompat.ActionStop))
);
this.StartForeground(NOTIFICATION_ID, builder.Build());
Here's what I have looked at so far to try to solve this:
When I start playback, I use MediaSession.setActive(true)
Each time I start and stop playback, I set the appropriate actions in PlaybackStateCompat
I have the session token set correctly.
I do not have anything set up as a MediaButtonReceiver in my manifest, nor have I set anything up to handle android.intent.action.MEDIA_BUTTON, because I am targeting Android 5.0 and higher and using the *Compat classes, and my understanding is that that is no longer necessary.
I know that media button events are being routed properly to my app, since my Bluetooth headphone buttons work. I tried it in my car and it works there too. It's just the buttons in the notification that won't work. I'm expecting them to generate calls to the appropriate methods of MediaSessionCompat.Callback. Is this incorrect? What am I doing wrong here?
I would be grateful for any pointers.
UPDATE:
I got it working. I needed to add the following inside the <application> node of the manifest:
<receiver android:name="android.support.v4.media.session.MediaButtonReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
...and the following inside the node of the Service that implements MediaBrowserServiceCompat:
<intent-filter>
<action android:name="android.media.browse.MediaBrowserService"/>
</intent-filter>
I'm still a little confused about why this was necessary, since button presses from my Bluetooth headphones and car infotainment system were routed to the app just fine. More importantly, Google says:
If you already have a MediaBrowserServiceCompat in your app,
MediaButtonReceiver will deliver the received key events to the
MediaBrowserServiceCompat by default. You can handle them in your
MediaSessionCompat.Callback.
They gave this as an alternative to the option "Service Handling ACTION_MEDIA_BUTTON," so I took that to mean I didn't need to do anything more with my manifest. If anyone could enlighten me here, I would appreciate it.
But, for what it's worth, this worked for me.

Probably you have not set up the actions. Look the code below, it show how to bind the buttons with an intent. Please modify it for android O devices which require a channel.
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.media.MediaDescriptionCompat;
import android.support.v4.media.MediaMetadataCompat;
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v4.media.session.PlaybackStateCompat;
import android.support.v7.app.NotificationCompat;
/**
* Keeps track of a notification and updates it automatically for a given MediaSession. This is
* required so that the music service don't get killed during playback.
*/
public class MediaNotificationManager extends BroadcastReceiver {
private static final int NOTIFICATION_ID = 412;
private static final int REQUEST_CODE = 100;
private static final String ACTION_PAUSE = "com.example.android.musicplayercodelab.pause";
private static final String ACTION_PLAY = "com.example.android.musicplayercodelab.play";
private static final String ACTION_NEXT = "com.example.android.musicplayercodelab.next";
private static final String ACTION_PREV = "com.example.android.musicplayercodelab.prev";
private final MusicService mService;
private final NotificationManager mNotificationManager;
private final NotificationCompat.Action mPlayAction;
private final NotificationCompat.Action mPauseAction;
private final NotificationCompat.Action mNextAction;
private final NotificationCompat.Action mPrevAction;
private boolean mStarted;
public MediaNotificationManager(MusicService service) {
mService = service;
String pkg = mService.getPackageName();
PendingIntent playIntent =
PendingIntent.getBroadcast(
mService,
REQUEST_CODE,
new Intent(ACTION_PLAY).setPackage(pkg),
PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent pauseIntent =
PendingIntent.getBroadcast(
mService,
REQUEST_CODE,
new Intent(ACTION_PAUSE).setPackage(pkg),
PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent nextIntent =
PendingIntent.getBroadcast(
mService,
REQUEST_CODE,
new Intent(ACTION_NEXT).setPackage(pkg),
PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent prevIntent =
PendingIntent.getBroadcast(
mService,
REQUEST_CODE,
new Intent(ACTION_PREV).setPackage(pkg),
PendingIntent.FLAG_CANCEL_CURRENT);
mPlayAction =
new NotificationCompat.Action(
R.drawable.ic_play_arrow_white_24dp,
mService.getString(R.string.label_play),
playIntent);
mPauseAction =
new NotificationCompat.Action(
R.drawable.ic_pause_white_24dp,
mService.getString(R.string.label_pause),
pauseIntent);
mNextAction =
new NotificationCompat.Action(
R.drawable.ic_skip_next_white_24dp,
mService.getString(R.string.label_next),
nextIntent);
mPrevAction =
new NotificationCompat.Action(
R.drawable.ic_skip_previous_white_24dp,
mService.getString(R.string.label_previous),
prevIntent);
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_NEXT);
filter.addAction(ACTION_PAUSE);
filter.addAction(ACTION_PLAY);
filter.addAction(ACTION_PREV);
mService.registerReceiver(this, filter);
mNotificationManager =
(NotificationManager) mService.getSystemService(Context.NOTIFICATION_SERVICE);
// Cancel all notifications to handle the case where the Service was killed and
// restarted by the system.
mNotificationManager.cancelAll();
}
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
switch (action) {
case ACTION_PAUSE:
mService.mCallback.onPause();
break;
case ACTION_PLAY:
mService.mCallback.onPlay();
break;
case ACTION_NEXT:
mService.mCallback.onSkipToNext();
break;
case ACTION_PREV:
mService.mCallback.onSkipToPrevious();
break;
}
}
public void update(
MediaMetadataCompat metadata,
PlaybackStateCompat state,
MediaSessionCompat.Token token) {
if (state == null
|| state.getState() == PlaybackStateCompat.STATE_STOPPED
|| state.getState() == PlaybackStateCompat.STATE_NONE) {
mService.stopForeground(true);
try {
mService.unregisterReceiver(this);
} catch (IllegalArgumentException ex) {
// ignore receiver not registered
}
mService.stopSelf();
return;
}
if (metadata == null) {
return;
}
boolean isPlaying = state.getState() == PlaybackStateCompat.STATE_PLAYING;
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mService);
MediaDescriptionCompat description = metadata.getDescription();
notificationBuilder
.setStyle(
new NotificationCompat.MediaStyle()
.setMediaSession(token)
.setShowActionsInCompactView(0, 1, 2))
.setColor(
mService.getApplication().getResources().getColor(R.color.notification_bg))
.setSmallIcon(R.drawable.ic_notification)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setContentIntent(createContentIntent())
.setContentTitle(description.getTitle())
.setContentText(description.getSubtitle())
.setLargeIcon(MusicLibrary.getAlbumBitmap(mService, description.getMediaId()))
.setOngoing(isPlaying)
.setWhen(isPlaying ? System.currentTimeMillis() - state.getPosition() : 0)
.setShowWhen(isPlaying)
.setUsesChronometer(isPlaying);
// If skip to next action is enabled
if ((state.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) != 0) {
notificationBuilder.addAction(mPrevAction);
}
notificationBuilder.addAction(isPlaying ? mPauseAction : mPlayAction);
// If skip to prev action is enabled
if ((state.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_NEXT) != 0) {
notificationBuilder.addAction(mNextAction);
}
Notification notification = notificationBuilder.build();
if (isPlaying && !mStarted) {
mService.startService(new Intent(mService.getApplicationContext(), MusicService.class));
mService.startForeground(NOTIFICATION_ID, notification);
mStarted = true;
} else {
if (!isPlaying) {
mService.stopForeground(false);
mStarted = false;
}
mNotificationManager.notify(NOTIFICATION_ID, notification);
}
}
private PendingIntent createContentIntent() {
Intent openUI = new Intent(mService, MusicPlayerActivity.class);
openUI.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
return PendingIntent.getActivity(
mService, REQUEST_CODE, openUI, PendingIntent.FLAG_CANCEL_CURRENT);
}
}

Related

Android: Stop Foreground Service causing Application crash

Prerequisites:
As a part of the requirement for my application, I need to make sure that the application won't be closed (killed) by the Android system while in background. For this purpose I implemented Foreground service, even though I don't do any actual process in background, just maintaining the state of the application. Everything works just fine, except one thing which is not fully clear to me how to fix.
The issue:
Sometimes (only once, for now), I receive this exception:
android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground():
This exception is thrown when I'm trying to stop the foreground service while it wasn't actually started.
So, my question is - is there is a way to stop foreground service properly, making sure that it is not running before actually stopping it?
What I found at the moment is that I can have static instance for my service and compare to null before stopping service, or get the list of all services currently running. But all these look like some "hack" workarounds.
Here some code:
MyForegroundService:
public class ForegroundService extends Service {
public static final int NOTIFICATION_ID = 1;
public static final String CHANNEL_ID = "SessionForegroundServiceChannel";
public static final String ACTION_FOREGROUND_START = "ACTION_FOREGROUND_START";
public static final String ACTION_FOREGROUND_STOP = "ACTION_FOREGROUND_STOP";
public static void startForegroundService(Context context) {
Intent intent = new Intent(context, ForegroundService.class);
intent.setAction(ForegroundService.ACTION_FOREGROUND_START);
ContextCompat.startForegroundService(context, intent);
}
public static void stopForegroundService(Context context) {
Intent intent = new Intent(context, ForegroundService.class);
intent.setAction(ForegroundService.ACTION_FOREGROUND_STOP);
ContextCompat.startForegroundService(context, intent);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (ACTION_FOREGROUND_START.equals(intent.getAction())) {
createNotificationChannel();
Intent stopForegroundIntent = new Intent(this, ForegroundServiceBroadcastReceiver.class);
PendingIntent pendingLogoutIntent = PendingIntent.getBroadcast(this,
0, stopForegroundIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
? null
: getString(R.string.app_short_name))
.setContentText(getString(R.string.foreground_description))
.setColor(getResources().getColor(R.color.color))
.setSmallIcon(R.drawable.ic_notification)
.addAction(R.drawable.ic_logout, getString(R.string.logout), pendingLogoutIntent)
.build();
startForeground(NOTIFICATION_ID, notification);
} else if (ACTION_FOREGROUND_STOP.equals(intent.getAction())) {
stopForeground(true);
stopSelf();
}
return START_STICKY;
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(
CHANNEL_ID,
getString(R.string.app_name),
NotificationManager.IMPORTANCE_LOW
);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(serviceChannel);
}
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
<service
android:name=".ui.ForegroundService"
android:exported="false"
android:stopWithTask="true"/>
I also have BroadcastReceiver and EventBus to listen to some events and stop foreground depending on those events.
Can you please help me, guys?
Let me add more details to what #Pawel commented:
You get this exception if you don't call Service.startForeground within 3 seconds of calling Context.startForegroundService that's all there's to it.
Here is how the complete solution will look like:
When it comes to the case when you need to stop a foreground service you need to do the following (pseudo code):
if (action == START_FOREGROUND) {
...
startForeground(NOTIFICATION_ID, notification);
} else if (action == STOP_FOREGROUND) {
startForeground(NOTIFICATION_ID, closeNotification); //in case it wasn't started before
stopForeground(true);
stopSelf();
}
Even though it is not obvious, and any documentation don't directly say that when you need to stop foreground you need to start foreground before stopping it (if it wasn't started).
Thanks #Pawel for the hint.

Notification Action without starting new Activity?

I plan to have a heads up notification that has two Actions: one to Approve a login request and one to Decline a login request. By clicking on either of these actions I wish to fire off a HTTP request to my server and most importantly do not want to start a new Activity or have the user redirected to my app at all.
Context context = getBaseContext();
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.notificationicon)
.setContentTitle(notificationTitle)
.setContentText("Access Request for " + appName + " : " + otp)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.addAction(R.drawable.ic_tick, "Approve", someApproveIntent? );
Here is my notification builder and after looking around it seems that the addAction method is looking for a new/pendingIntent, which is confusing me as I cannot find any examples online where Intents do not lead to new Activities being fired off.
How would I implement some code (a method maybe) rather then starting a new Activity on each of my Actions?
If you don't want to start an activity you can also wrap a BroadcastReceiver or a Service directly in a PendingIntent.
Wherever you build your notification...
Your notification actions will start a service directly.
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)...
Intent iAction1 = new Intent(context, MyService.class);
iAction1.setAction(MyService.ACTION1);
PendingIntent piAction1 = PendingIntent.getService(context, 0, iAction1, PendingIntent.FLAG_UPDATE_CURRENT);
builder.addAction(iconAction1, titleAction1, piAction1);
// Similar for action 2.
MyService.java
IntentServices run in a row one after another. They do the work on a worker thread.
public class MyService extends IntentService {
public static final String ACTION1 = "ACTION1";
public static final String ACTION2 = "ACTION2";
#Override
public void onHandleIntent(Intent intent) {
final String action = intent.getAction();
if (ACTION1.equals(action)) {
// do stuff...
} else if (ACTION2.equals(action)) {
// do some other stuff...
} else {
throw new IllegalArgumentException("Unsupported action: " + action);
}
}
}
AndroidManifest.xml
Don't forget to register the service in manifest.
<manifest>
<application>
<service
android:name="path.to.MyService"
android:exported="false"/>
</application>
</manifest>

How to know make notification when user doesn't executing application

I'd like to make an notification which start to count time when user exited android application. If user do not executed application after 1hours, It notified me to execute and If user ignoring it, It executes saved SMS messages. I found some examples on timer, but I do not know how to find application exit time. Please give me some advice with full code. I am desperately need it...
TimerTask task = new TimerTask(){
public void run() {
try {
mainTime++;
int min = mainTime / 60;
int sec = mainTime % 60;
String strTime = String.format("%s : %s", min, sec);
} catch (Exception e) {
e.printStackTrace();
}
}
};
Timer mTimer = new Timer();
mTimer.schedule(task, 0, 60000);
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("Chack your app", smsBody);
sendIntent.putExtra("12345678", phonenumber);
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
Okay so what you need to do is to store the system time locally (may be using SharedPreferences) when the application exits. You can register a BroadcastReceiver which will help you trigger some action when 1hr or a certain time has passed from the locally stored time when app exited.
If you want to know how to handle programmatically when and how to exit the app , please refer this answer.
You could also try to use the Android alarm system. Once the user exit your application, you could set up an Alarm. Something like:
YourActivityOrFragment.java
#Override
protected void onStop() {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
c.add(Calendar.HOUR,1);
scheduleAlarm(c.getTimeInMillis());
}
private void scheduleAlarm(long time) {
Intent yourIntent = new Intent("Some_ID");
PendingIntent pi = PendingIntent.getBroadcast(YourClass.this, ALARM_ID, yourIntent, PendingIntent.FLAG_CANCEL_CURRENT);
// Put some extras here, if you need so. Like:
// yourIntent.putExtra("field","value");
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,time,pi);
}
Now, create a BroadcastReceiver to handle those alarms.
AlarmReceiver.java
public class AlarmReceiver extends BroadcastReceiver {
private static final String LOG_TAG = AlarmReceiver.class.getSimpleName();
#Override
public void onReceive(Context context, Intent intent) {
Log.d(LOG_TAG, "Alarm fired!");
Intent it = new Intent(context, YourNotificationHandler.class);
// Get your Extras here. And do whatever you want, if you need.
// For what you said, there's no need to start an Activity, so let's handle that alarm as a service.
context.startService(it);
// But if for some reason you want to start an Activity, just do it like:
// context.startActivity(it);
}
}
On your AndroidManifest.xml declare your BroadcastReceiver.
<receiver android:name=".AlarmReceiver" >
<intent-filter>
<action android:name="Some_ID" />
<category android:name="android.intent.category.default" />
</intent-filter>
</receiver>
And last of all, create your service to handle your notifications, you could try something like an IntentService. On that file, you'll have a onHandleIntent(Intent intent) method. Get your Intent there, and it's Extras, and do whatever you want to do. Later, just call your Notifications. I've used a utility class on my projects to handle those, but feel free to choose how you'll do that.
Example:
public static void createService(Context context, CharSequence tickerMessage, CharSequence title,
CharSequence message, int icon, int id, Intent intent, long[] pattern, Boolean autoCancel) {
PendingIntent p = PendingIntent.getService(context, 0, intent, 0);
Notification n;
int apiLevel = Build.VERSION.SDK_INT;
if (apiLevel >= 11) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentTitle(title)
.setTicker(tickerMessage)
.setContentText(message)
.setSmallIcon(icon)
.setContentIntent(p)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
if (pattern.length > 0) {
builder.setVibrate(pattern);
}
if (autoCancel != null) {
builder.setAutoCancel(autoCancel);
}
if (apiLevel >= 17) {
// Android 4.2+
n = builder.build();
}
else {
// Android 3.x
n = builder.getNotification();
}
}
else {
// Android 2.2+
n = new Notification(icon, tickerMessage, System.currentTimeMillis());
// Data
n.setLatestEventInfo(context, title, message, p);
}
NotificationManager nm = (NotificationManager)
context.getSystemService(Activity.NOTIFICATION_SERVICE);
nm.notify(id, n);
}
You can read more about alarms here.
More on Service here.
BroadcastReceiver here.
Notifications, here and here.
And this might be an interesting read about Notification as well.

android notification in background if app closed?

I am trying to display a notification in the Android notifications bar even if my application is closed.
I've tried searching, but I have had no luck finding help.
An example of this is a news application. Even if the phone screen is off or the news application is closed, it can still send a notification for recent news and have it appear in the notification bar.
How might I go about doing this in my own application?
You have to build a Service that handles your news and shows notifications when it knows that are new news (Service Doc).
The service will run in background even if your application is closed.
You need a BroadcastReciever to run the service in background after the boot phase is completed. (Start service after boot).
The service will build your notifications and send them through the NotificationManager.
EDIT: This article may suit your needs
The selected answer is still correct, but only for devices running Android 7 versions and below.
As of Android 8+, you can no longer have a service running in the background while your app is idle/closed.
So, it now depends on how you set up your notifications from your GCM/FCM server. Ensure to set it to the highest priority. If your app is in the background or just not active and you only send notification data, the system process your notification and send it to the Notification tray.
I used this answer to write a service, and as an exmaple you need to call ShowNotificationIntentService.startActionShow(getApplicationContext()) inside one of your activities:
import android.app.IntentService;
import android.content.Intent;
import android.content.Context;
public class ShowNotificationIntentService extends IntentService {
private static final String ACTION_SHOW_NOTIFICATION = "my.app.service.action.show";
private static final String ACTION_HIDE_NOTIFICATION = "my.app.service.action.hide";
public ShowNotificationIntentService() {
super("ShowNotificationIntentService");
}
public static void startActionShow(Context context) {
Intent intent = new Intent(context, ShowNotificationIntentService.class);
intent.setAction(ACTION_SHOW_NOTIFICATION);
context.startService(intent);
}
public static void startActionHide(Context context) {
Intent intent = new Intent(context, ShowNotificationIntentService.class);
intent.setAction(ACTION_HIDE_NOTIFICATION);
context.startService(intent);
}
#Override
protected void onHandleIntent(Intent intent) {
if (intent != null) {
final String action = intent.getAction();
if (ACTION_SHOW_NOTIFICATION.equals(action)) {
handleActionShow();
} else if (ACTION_HIDE_NOTIFICATION.equals(action)) {
handleActionHide();
}
}
}
private void handleActionShow() {
showStatusBarIcon(ShowNotificationIntentService.this);
}
private void handleActionHide() {
hideStatusBarIcon(ShowNotificationIntentService.this);
}
public static void showStatusBarIcon(Context ctx) {
Context context = ctx;
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx)
.setContentTitle(ctx.getString(R.string.notification_message))
.setSmallIcon(R.drawable.ic_notification_icon)
.setOngoing(true);
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, STATUS_ICON_REQUEST_CODE, intent, 0);
builder.setContentIntent(pIntent);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notif = builder.build();
notif.flags |= Notification.FLAG_ONGOING_EVENT;
mNotificationManager.notify(STATUS_ICON_REQUEST_CODE, notif);
}
}

Show a notification for missed VOIP calls

I did not find an answer to this question.
I have a VOIP application. I'm able to add a log about missed calls in the native call-log but the notification is not shown.
Is there a way to ask the device native call log to show the notification?
I would not like to add my own notification because I want to be sure that the icon is always that one that the native call-log application would show for other calls.
I found this but there is no answer.
I'm using a code that is similar to that one shown in the previous post.
Just for clarity:
You can still add your own notification, but use the android system build-in icons - these are the same icons that the built-in phonecall app is using. For example:
Notification notification = new Notification();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.icon = android.R.drawable.stat_notify_missed_call;
This way you will always display the icon that's characteristic for the specific Android OS version.
For more information check out the Icon Guidelines, and especially in your case -the status bar icons.
If you are just adding an entry in the call log db the notificatin will not be shown.
You need to add your on notification.
If you do not add your own notification, a notification will be shown only at phone start up if the call log is still there.
This is my implementation (mainly copied from the android code):
private static void showMissedCallNotification(Context context, final Contact contact) {
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// display the first line of the notification:
// 1 missed call: call name
// more than 1 missed call: <number of calls> + "missed calls"
int titleResId;
String expandedText;
numberMissedCalls++;
if (numberMissedCalls == 1) {
titleResId = R.string.notification_missedCallTitle;
expandedText = contact.getDisplayName();
} else {
titleResId = R.string.notification_missedCallsTitle;
expandedText = context.getString(R.string.notification_missedCallsMsg,
numberMissedCalls);
}
final PendingIntent callLogIntent = createCallLogIntent(context);
// make the notification
int id = android.R.drawable.stat_notify_missed_call;
String ticker = context.getString(R.string.notification_missedCallTicker, contact.getDisplayNumber());
long currentTime = Platform.timeProvider().getTime();
Notification note = new Notification(id, ticker, currentTime);
note.setLatestEventInfo(context, context.getText(titleResId), expandedText, callLogIntent);
note.flags |= Notification.FLAG_AUTO_CANCEL;
// This intent will be called when the notification is dismissed.
// It will take care of clearing the list of missed calls.
note.deleteIntent = createClearMissedCallsIntent(context);
//configureLedNotification(note);
notificationManager.notify(MISSED_CALL_NOTIFICATION, note);
}
/**
* Returns an intent to be invoked when the missed call notification is clicked.
* #param context
*/
private static PendingIntent createCallLogIntent(Context context) {
Intent intent = new Intent(context, ClearMissedCallsService.class);
intent.setAction(ClearMissedCallsService.ACTION_OPEN_CALL_LOGS);
return PendingIntent.getService(context, 0, intent, 0);
}
/**
* Returns an intent to be invoked when the missed call notification is cleared.
* #param context
*/
private static PendingIntent createClearMissedCallsIntent(Context context) {
Intent intent = new Intent(context, ClearMissedCallsService.class);
intent.setAction(ClearMissedCallsService.ACTION_CLEAR_MISSED_CALLS);
return PendingIntent.getService(context, 0, intent, 0);
}
/*package */ static void cancelMissedCallNotification() {
// reset the number of missed calls to 0.
numberMissedCalls = 0;
notificationManager.cancel(MISSED_CALL_NOTIFICATION);
}
and:
/**
* Handles the intent to clear the missed calls that is triggered when a notification is dismissed.
*/
public class ClearMissedCallsService extends IntentService {
/** This action is used to clear missed calls. */
public static final String ACTION_CLEAR_MISSED_CALLS = "com.android.phone.intent.CLEAR_MISSED_CALLS";
public static final String ACTION_OPEN_CALL_LOGS = "com.android.phone.intent.OPEN_CALL_LOGS";
public ClearMissedCallsService() {
super(ClearMissedCallsService.class.getSimpleName());
}
#Override
public void onCreate() {
super.onCreate();
}
#Override
protected void onHandleIntent(Intent intent) {
// Clear the list of new missed calls.
ContentValues values = new ContentValues();
values.put(Calls.NEW, 0);
StringBuilder where = new StringBuilder();
where.append(Calls.NEW);
where.append(" = 1 AND ");
where.append(Calls.TYPE);
where.append(" = ?");
getContentResolver().update(Calls.CONTENT_URI, values, where.toString(),
new String[]{ Integer.toString(Calls.MISSED_TYPE) });
NativeCallLog.cancelMissedCallNotification();
if (ACTION_OPEN_CALL_LOGS.equals(intent.getAction())) {
Intent intentOpenCallLogs = createOpenCallLogIntent();
startActivity(intentOpenCallLogs);
}
}
private static Intent createOpenCallLogIntent() {
Intent intent = new Intent(Intent.ACTION_VIEW, null);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setType("vnd.android.cursor.dir/calls");
return intent;
}
}
you also need to add this in the AndroidManifest
<service
android:exported="true"
android:name="yourpackage.ClearMissedCallsService" >
<intent-filter >
<action android:name="com.android.phone.intent.CLEAR_MISSED_CALLS" />
</intent-filter>
<intent-filter >
<action android:name="com.android.phone.intent.OPEN_CALL_LOGS" />
</intent-filter>
</service>

Categories

Resources