I'm coding an android app for my school, and i have a little problem with notifications:
My app checks if the homepage was updated (date of page is newer than date of last view) in background. If the page was updated, the app builds a notifications (till here it works fine) with 2 options: Dismiss or show (also works). "Show" works fine, but my problem is "Dismiss" because it has to update the date in settings and it should quit then, but it's always showing the last opened activity.
I'm having an Activity which manages this with Intent extras:
public class ***********Handler extends Activity {
String EXTRA_ACTION = "de.xorg.*****.ACTION";
String EXTRA_VPDATE = "de.xorg.*****.VPDATE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ntd);
Intent intent = getIntent();
String todo = intent.getStringExtra(EXTRA_ACTION); //Action from Notification ("D" or "S")
String date = intent.getStringExtra(EXTRA_VPDATE); //New date
if(todo.toLowerCase().contains("d")) {
//update date value
Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
editor.putString("readDate", date);
editor.commit();
// Remove notification
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(0);
// Quit app (1st method) [I'm only using one in real code, but both aren't working]
android.os.Process.killProcess(android.os.Process.myPid());
// Quit app (2nd method)
finish();
} else {
//update date value
Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
editor.putString("readDate", date);
editor.commit();
// Remove notification
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(0);
//Set theme (not important now)
Boolean BeanUI = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("bean", false);
String URL;
if(BeanUI) {
URL = "*****" + PreferenceManager.getDefaultSharedPreferences(this).getString("klasse", "") + "&fc=ffffff&bc=000000";
} else {
URL = "*****" + PreferenceManager.getDefaultSharedPreferences(this).getString("klasse", "");
}
String EXTRA_URL = "de.xorg.*****.MESSAGE";
String EXTRA_NAME = "de.xorg.*****.MESSAGENAME";
Intent intent2 = new Intent(this, InternetViewer.class);
intent2.putExtra(EXTRA_URL, URL);
intent2.putExtra(EXTRA_NAME, ",Vertretungsplan");
startActivity(intent2);
}
}
}
And here's the code which creates the notification:
// I've censored all private values with ***'s
// CheckService.MC = Context of main activity
// CheckService.NM = NotificationManager
public static void PostNotification(String text, String datum) {
Intent intentS = new Intent(CheckService.MC, ***************Handler.class);
Intent intentD = new Intent(CheckService.MC, ***************Handler.class);
String EXTRA_ACTION = "de.xorg.*****.ACTION";
String EXTRA_DATE = "de.xorg.*****.VPDATE";
intentS.putExtra(EXTRA_ACTION, "S");
intentS.putExtra(EXTRA_DATE, datum);
intentD.putExtra(EXTRA_ACTION, "D");
intentD.putExtra(EXTRA_DATE, datum);
PendingIntent pIntentD = PendingIntent.getActivity(CheckService.MC, 0, intentD, 0);
PendingIntent pIntentS = PendingIntent.getActivity(CheckService.MC, 0, intentS, 0);
// Build notification
Notification n = new Notification.Builder(CheckService.MC)
.setContentTitle("Title")
.setContentText(text)
.setSmallIcon(R.drawable.vertretung)
.setContentIntent(pIntentS)
.setAutoCancel(true)
.addAction(R.drawable.anzeigen, "Show", pIntentS)
.addAction(R.drawable.gelesen, "Dismiss", pIntentD).build();
CheckService.NM.notify(0, n);
}
I've tried to do it with »finish();« and with »android.os.Process.killProcess(android.os.Process.myPid());« both aren't working.
Why doesn't that work? Is there a different (better) way to update the setting?
Related
I am written on Custom plugin,Which works only for handling the sticky notification plugin events.
Working Scenario and Issues I am getting:-
It works fine when install in the demo corodva application and functionality also works fine like after closing app sticky notification created by the custom plugin appear on locak screen and notification bar.
But when trying to install phonegap push plugin in the demo application in which custom plugin already install. After that sticky notification gets clear/remove/disappear from notification bar.When the app closed by the user.
When I see the Phonegap Push Plugin source code for android the have written NotifcationManger.cancelAll() in the plugin.
Not getting why this is happen?
Adding my sticky notification plugin code below:-
public class StickyNotificationPlugin extends CordovaPlugin {
private static final String START_NOTIFICATION = "START";
private static final String STOP_NOTIFICATION = "STOP";
private Notification myNotication;
int intNotificationId = 11111;
private NotificationManager manager;
private SharedPreferences objSharedPreferences;
#Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
objSharedPreferences = this.cordova.getActivity().getSharedPreferences("My_Plugin_Prefs", Context.MODE_PRIVATE);
System.out.println("Final in Plugin Action==>" + action);
manager = (NotificationManager) this.cordova.getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
if (START_NOTIFICATION.equals(action)) {
SharedPreferences.Editor objEditor = objSharedPreferences.edit();
objEditor.putString("plugin_url", args.getString(0));
objEditor.putString("plugin_token", args.getString(1));
objEditor.putString("plugin_user_id", args.getString(2));
objEditor.putBoolean("plugin_status", true);
objEditor.commit();
Notify();
} else if (STOP_NOTIFICATION.equals(action)) {
SharedPreferences.Editor objEditor = objSharedPreferences.edit();
objEditor.putString("plugin_url", "");
objEditor.putString("plugin_token", "");
objEditor.putString("plugin_user_id", "");
objEditor.putBoolean("plugin_status", false);
objEditor.putLong("time", 0);
objEditor.commit();
manager.cancel(intNotificationId);
}
return true;
}
private void Notify() {
Context objContext = this.cordova.getActivity();
Intent objIntent = new Intent(objContext, ApiCallServeice.class);
PendingIntent pi = PendingIntent.getService(objContext, intNotificationId, objIntent, PendingIntent.FLAG_CANCEL_CURRENT);
RemoteViews objRemoteViews = new RemoteViews(objContext.getApplicationContext().getPackageName(), R.layout.sticky_notification);
objRemoteViews.setOnClickPendingIntent(R.id.btn_notification, pi);
Notification.Builder builder = new Notification.Builder(objContext);
builder.setAutoCancel(false);
builder.setSmallIcon(objContext.getApplicationInfo().icon);
objRemoteViews.setImageViewResource(R.id.img_icon, objContext.getApplicationInfo().icon);
builder.setOngoing(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder.setVisibility(Notification.VISIBILITY_PUBLIC);
}
builder.setContent(objRemoteViews);
builder.build();
myNotication = builder.getNotification();
manager.notify(intNotificationId, myNotication);
}}
Your problem will be solved by removing the two lines that cancels the notifications once the home button is pressed -
These two lines are found in the onPause() method in PushPlugin.java file.
final NotificationManager notificationManager = (NotificationManager) cordova.getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();
I have followed tutorial in http://developer.android.com/training/notify-user/index.html
It works well. But what I want is : when I click ping, the old service will we stopped, and then create the service again. So if I clicked id multiple time, It will notify me only once.
Problem: If I set time 10, then I click "Ping" button. Then after 5 second, I click it again. It will notify me twice.
What I want : If I set time 10, then I click "Ping" button. Then after 5 second, I click it it will notify only once, 10 secondds after the last time I click the button.
public class MainActivity extends Activity {
private Intent mServiceIntent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Creates an explicit Intent to start the service that constructs and
// issues the notification.
mServiceIntent = new Intent(getApplicationContext(), PingService.class);
}
/*
* Gets the values the user entered and adds them to the intent that will be
* used to launch the IntentService that runs the timer and issues the
* notification.
*/
public void onPingClick(View v) {
stopCurrentService();
int seconds;
// Gets the reminder text the user entered.
EditText msgText = (EditText) findViewById(R.id.edit_reminder);
String message = msgText.getText().toString();
mServiceIntent.putExtra(CommonConstants.EXTRA_MESSAGE, message);
mServiceIntent.setAction(CommonConstants.ACTION_PING);
Toast.makeText(this, R.string.timer_start, Toast.LENGTH_SHORT).show();
// The number of seconds the timer should run.
EditText editText = (EditText) findViewById(R.id.edit_seconds);
String input = editText.getText().toString();
if (input == null || input.trim().equals("")) {
// If user didn't enter a value, sets to default.
seconds = R.string.seconds_default;
} else {
seconds = Integer.parseInt(input);
}
int milliseconds = (seconds * 1000);
mServiceIntent.putExtra(CommonConstants.EXTRA_TIMER, milliseconds);
// Launches IntentService "PingService" to set timer.
startService(mServiceIntent);
}
private void stopCurrentService() {
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> serviceList = activityManager
.getRunningServices(Integer.MAX_VALUE);
if (serviceList.size() <= 0) { }
int size = serviceList.size();
for (int i = 0; i < size; i++) {
RunningServiceInfo serviceInfo = serviceList.get(i);
ComponentName serviceName = serviceInfo.service;
if (serviceName.getClassName().equals(PingService.class.getName())) {
try {
Intent intentstop = new Intent();
intentstop.setComponent(serviceName);
getApplicationContext().stopService(intentstop);
} catch (SecurityException e) {
e.printStackTrace();
}
}
}
}
}
PingService creates a notification that includes 2 buttons: one to snooze the
notification, and one to dismiss it.
public class PingService extends IntentService {
private NotificationManager mNotificationManager;
private String mMessage;
private int mMillis;
NotificationCompat.Builder builder;
private boolean status;
public PingService() {
// The super call is required. The background thread that IntentService
// starts is labeled with the string argument you pass.
super("com.example.android.pingme");
}
#Override
protected void onHandleIntent(Intent intent) {
// The reminder message the user set.
mMessage = intent.getStringExtra(CommonConstants.EXTRA_MESSAGE);
// The timer duration the user set. The default is 10 seconds.
mMillis = intent.getIntExtra(CommonConstants.EXTRA_TIMER,
CommonConstants.DEFAULT_TIMER_DURATION);
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
String action = intent.getAction();
// This section handles the 3 possible actions:
// ping, snooze, and dismiss.
if (action.equals(CommonConstants.ACTION_PING)) {
issueNotification(intent, mMessage);
} else if (action.equals(CommonConstants.ACTION_SNOOZE)) {
nm.cancel(CommonConstants.NOTIFICATION_ID);
Log.d(CommonConstants.DEBUG_TAG, getString(R.string.snoozing));
// Sets a snooze-specific "done snoozing" message.
issueNotification(intent, getString(R.string.done_snoozing));
} else if (action.equals(CommonConstants.ACTION_DISMISS)) {
nm.cancel(CommonConstants.NOTIFICATION_ID);
}
}
private void issueNotification(Intent intent, String msg) {
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Sets up the Snooze and Dismiss action buttons that will appear in the
// expanded view of the notification.
Intent dismissIntent = new Intent(this, PingService.class);
dismissIntent.setAction(CommonConstants.ACTION_DISMISS);
PendingIntent piDismiss = PendingIntent.getService(this, 0,
dismissIntent, 0);
Intent snoozeIntent = new Intent(this, PingService.class);
snoozeIntent.setAction(CommonConstants.ACTION_SNOOZE);
PendingIntent piSnooze = PendingIntent.getService(this, 0,
snoozeIntent, 0);
// Constructs the Builder object.
builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_notification)
.setTicker("Ping ! ping ! PIng!")
.setContentTitle(getString(R.string.notification))
.setContentText(getString(R.string.ping))
.setDefaults(Notification.DEFAULT_ALL)
// requires VIBRATE permission
/*
* Sets the big view "big text" style and supplies the text (the
* user's reminder message) that will be displayed in the detail
* area of the expanded notification. These calls are ignored by
* the support library for pre-4.1 devices.
*/
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.addAction(R.drawable.ic_stat_dismiss,
getString(R.string.dismiss), piDismiss)
.addAction(R.drawable.ic_stat_snooze,
getString(R.string.snooze), piSnooze);
/*
* Clicking the notification itself displays ResultActivity, which
* provides UI for snoozing or dismissing the notification. This is
* available through either the normal view or big view.
*/
Intent resultIntent = new Intent(this, ResultActivity.class);
resultIntent.putExtra(CommonConstants.EXTRA_MESSAGE, msg);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
// Because clicking the notification opens a new ("special") activity,
// there's
// no need to create an artificial back stack.
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0,
resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
startTimer(mMillis);
}
// Starts the timer according to the number of seconds the user specified.
private void startTimer(int millis) {
Log.d(CommonConstants.DEBUG_TAG, getString(R.string.timer_start));
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
Log.d(CommonConstants.DEBUG_TAG, getString(R.string.sleep_error));
}
Log.d(CommonConstants.DEBUG_TAG, getString(R.string.timer_finished));
issueNotification(builder);
}
private void issueNotification(NotificationCompat.Builder builder) {
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Including the notification ID allows you to update the notification
// later on.
mNotificationManager.notify(CommonConstants.NOTIFICATION_ID,
builder.build());
}
#Override
public void onDestroy() {
super.onDestroy();
}
}
I have called stopService(), but the old notification shows up again.
What I want is it will notify me once, 10 seconds after the latest click.
You can use handler in order to stop/start your service.
Please look at my code. It's not exactly related to your code but you can get the idea.
Click this link
You can do checking in Run method of Runnable.
Currently I am working on GCM (Google Cloud message), it allow user to push the message to user device. And I would like achieve the following requirement :
if the user has already enter app , ignore it
if the user has not enter the app , click on notification to enter the app
And the work flow of my app is:
WelcomePage (download json and create data set from it) => MainPage (Display base on the data set)
The code to handle notification
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
String notifyMsg = "";
JSONTokener tokener = new JSONTokener(msg);
if (tokener != null) {
try {
notifyMsg = new JSONObject(tokener).getString("msg");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Intent myintent = new Intent(this, WelcomePageActivity.class);
myintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, myintent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getResources().getString(R.string.notification_title))
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(notifyMsg))
.setContentText(notifyMsg)
.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
The problem is if I use WelcomePageActivity class , it will create a new activity if I am at the main page, how can I adjust the code to fit my requirement ?
Thanks
For
1. if the user has already enter app , ignore it:
in the onReceive() , check if your app is running, do not notify.
It can be checked with something like:
ActivityManager activityManager =(ActivityManager)gpsService.this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> serviceList= activityManager.getRunningServices(Integer.MAX_VALUE);
if((serviceList.size() > 0)) {
boolean found = false;
for(int i = 0; i < serviceList.size(); i++) {
RunningServiceInfo serviceInfo = serviceList.get(i);
ComponentName serviceName = serviceInfo.service;
if(serviceName.getClassName().equals("Packagename.ActivityOrServiceName")) {
//Your service or activity is running
break;
}
}
if the user has not enter the app , click on notification to enter the app
from the code above, you'l know if you would like to resume the app or launch - call Splash Screen or in your case WelcomeActivity.
About the workflow of your app, i'd suggest check whether you need to download the data every time or not. Can save it maybe or update/download only when required, and rest of flow works as it is.
In your AndroidManifest.xml, define your WelcomePageActivity with the flag android:launchMode="singleTop". From the definition of this flag:
A new instance of a "singleTop" activity may also be created to handle
a new intent. However, if the target task already has an existing
instance of the activity at the top of its stack, that instance will
receive the new intent (in an onNewIntent() call); a new instance is
not created.
So with this flag, your activity will not be created again, rather it will receive a call in the onNewIntent() function with the Intent you used to create the PendingIntent for the notification. You could override this function, and use the intent to pass the activity new information.
You will not able to receive any notification click event so,
try this code :
Intent myintent = new Intent(this, TestActivity.class);
myintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, myintent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getResources().getString(R.string.notification_title))
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(notifyMsg))
.setContentText(notifyMsg)
.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
public class TestActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// check for your app state is running or not
if(appRunning == false) {
// start your WelcomePage activity.
}
}
}
1.Create an object in GcmIntentService
public static final Object CURRENTACTIVIYLOCK = new Object();
//for storing current activity
public static Activity currentActivity;
2.Update this object value in onPause and onResume of MainActivity to recognize Activity is running or not.
#Override
public void onResume() {
super.onResume();
System.out.println("onResume Home page");
synchronized (GcmIntentService.CURRENTACTIVIYLOCK) {
GcmIntentService.currentActivity = this;
}
}
#Override
public void onPause() {
super.onPause();
synchronized (GcmIntentService.CURRENTACTIVIYLOCK) {
GcmIntentService.currentActivity = null;
}
}
3.In GcmIntentService class, check for the current activity in onHandleIntent method.
synchronized (CURRENTACTIVIYLOCK) {
if (currentActivity != null) {
if (currentActivity.getClass() == HomePageActivity.class) {
} else {
sendNotification(extras.getString("message"));
}
} else {
sendNotification(extras.getString("message"));
}
I'm sure this will help you.
May be this is a childish question, but i am beginer please do not consider as wrong question., i was searching alot, also m following this
http://developer.android.com/guide/topics/ui/notifiers/notifications.html tutorial. and also google to solve my problem but i could't find.
This is my assignment.
I have a EditText and a Button in main activity, When i click the button a notification is generated, when i am opening that notification another activity is open, shows Editext data, which i have entered in the main activity through EditText.
my Question is....
I want to Show the count of pending notifications in a single Notification window as in http://developer.android.com/guide/topics/ui/notifiers/notifications.html#Updating
but i could't understand this --> Start of a loop that processes data and then notifies the user....
how can i achive this. how can i process data to get the pending count. keep in mind this count will decrement when there is no notification???
When click a notification than i want to get all notifications in a separate activity just like a messages in inbox.
e.g. in my main activity i am clicking the button 10 times, so actually 10 notifications will generate in a single notification window with count=10, but it shows count = 1?? when i open the notifications than it will only show the latest notification contents in another activity, how can i show remaining 9 in a single activity??
Below in my Main activity....
Button btn;
EditText edtText;
NotificationCompat.Builder builder;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
edtText = (EditText) findViewById(R.id.editText);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
CreteNotification(Calendar.getInstance().getTimeInMillis(), edtText.getText().toString());
}
});
}
protected void CreteNotification(long when, String data) {
String notificationContent ="Notification Content Click Here to go more details";
String notificationTitle ="This is Notification";
int number = 1;
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
int smalIcon =R.drawable.ic_launcher;
String notificationData = data;
Intent intent = new Intent(getApplicationContext(), MyNotificationClass.class);
intent.putExtra("Message", notificationData);
intent.putExtra("Time", Integer.toString((int) when) );
intent.setData(Uri.parse("content://"+when));
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager notificationManager =(NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext())
.setWhen(when)
.setContentText(notificationContent)
.setContentTitle(notificationTitle)
.setSmallIcon(smalIcon)
.setAutoCancel(true)
.setTicker(notificationTitle)
.setLargeIcon(largeIcon)
.setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_VIBRATE| Notification.DEFAULT_SOUND)
.setContentIntent(pendingIntent);
// Start of a loop that processes data and then notifies the user
// how to loop??????????????????
notificationBuilder.setNumber(++number);
Notification notification = notificationBuilder.getNotification();
notificationManager.notify(1, notification);
}
code where i want to show all notifications?????
HashMap<String, String> inboxMsg;
TextView notiTextView;
Button btn;
int Id;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_notification_class);
inboxMsg = new HashMap<String, String>();
Id = 0;
notiTextView = (TextView) findViewById(R.id.textView1);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
#SuppressWarnings("rawtypes")
#Override
public void onClick(View v) {
Set<String> keys = inboxMsg.keySet();
Iterator keyItra = keys.iterator();
while (keyItra.hasNext()) {
String k = (String) keyItra.next();
String Message = inboxMsg.get(k);
notiTextView.setText(Message);
}
}
});
if(savedInstanceState == null)
{
String Mesage = getIntent().getExtras().getString("Message");
String Time = getIntent().getExtras().getString("Time");
inboxMsg.put(Integer.toString(++Id), "Message is " + Mesage + " Time " + Time + "\n");
}
}
where is the problem kindly redirect me to the correct path, also kindly guide me how to achive this.
The reason why you are not getting the actual notification count is that whenever CreteNotification(long when, String data) method is called number variable is set to 1. It can be solved by instead of declaring it inside a method make number a class member variable.
Button btn;
EditText edtText;
NotificationCompat.Builder builder;
int number = 1;
.....
protected void CreteNotification(long when, String data) {
.....
notificationBuilder.setNumber(++number);
....
}
Regarding starting different activity for every notification, you need to pass different 'ID' for each notification when calling notificationManager.notify(ID, notification); method, but remember if you assign different ID, triggering new notification won't update the count but add a new notification. So clicking the button for generating notification will actually generate 10 different notifications.
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>