I am developing apps that can perform sending fake sms and receiving fake text message. It's better If I can use built-in SMS notification but I don't know the way. So I use my below code to generate Notification and when I click on that Notification nothing happens. Please can anyone solve my problem? I need to open Message Inbox when I click that notification. Any help would be appreciated!
Uri sms_uri = Uri.parse("smsto:+92xxxxxxxx");
Intent sms_intent = new Intent(Intent.ACTION_SENDTO, sms_uri);
sms_intent.putExtra("sms_body", "Good Morning ! how r U ?");
PendingIntent contentIntent = PendingIntent.getService(context, 0, sms_intent, 0);
Resources res = context.getResources();
Notification noti = new Notification.Builder(context)
.setContentTitle(res.getString(R.string.app_name))
//.setContentText(res.getString(R.string.cancelText))
.setSmallIcon(android.R.drawable.ic_dialog_email)
.setTicker(res.getString(R.string.app_name))
// .setAutoCancel(true)
.setWhen(System.currentTimeMillis())
.setContentIntent(contentIntent)
.build();
NotificationManager notificationManager = (NotificationManager) getActivity().getSystemService(NOTIFICATION_SERVICE);
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
Use
PendingIntent.getActivity(context, 0 , sms_intent, 0);
that may help!!!
Related
Whenever I receive a push notification on Android it replaces any existing notification in the drawer.
any idea to fix that?
Below is my code:
NotificationManager notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Intent intent = new Intent(DbInsertService.this, MessageActivity.class);
intent.putExtra("Service",serviceType);
PendingIntent pIntent = PendingIntent.getActivity(DbInsertService.this, (int)
System.currentTimeMillis(), intent,PendingIntent.FLAG_UPDATE_CURRENT);
Notification n = new Notification.Builder(DbInsertService.this)
.setContentTitle("Message from " +serviceType.getSer_name())
.setContentText(messageType.getHis_title())
.setSmallIcon(R.drawable.ic_email_variant_grey600_24dp)
.setContentIntent(pIntent)
.setAutoCancel(true).build();
notificationManager.notify(0, n);
thanks
Set different Id for different notification.
notificationManager.notify(0, n); // here set different Id.
You're sending them all with the same id. Send different ids and they won't overwrite.
I am using the following code for displaying notification . Upon displaying the notification , I go to the activity if I click in the notifications .
void Notify(String notificationTitle, String notificationMessage){
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.noti, notificationTitle,System.currentTimeMillis());
Intent notificationIntent = new Intent(this, SmsActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
notification.setLatestEventInfo(this, notificationTitle,notificationMessage, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}
Now I want to update the text of notifications . How can I do that ?
You can try this
//First time
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentText(context.getString(R.string.notif_text))
.setContentTitle(title)
.setSmallIcon(R.drawable.ic_action_alarm_2)
.setAutoCancel(false)
.setOngoing(running)
.setOnlyAlertOnce(true)
.setContentIntent(
PendingIntent.getActivity(context, 10,
new Intent(context, YourActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP),
0)
)
notificationManager.notify(id, builder.build());
//Second time
builder.setContentTitle(title);
notificationManager.notify(id, builder.build());
Send notification with same id:
https://developer.android.com/training/notify-user/managing.html
You can update the text of notification by sending different text in Notify(String notificationTitle, String notificationMessage) method.
The problem in approach suggested here is that if user already responded to this notification he could see it again, so you'd better to show it once instead.
So I wouldn't recommend to use this API at all in order to update your notifications.
If you are using it for that purpose you probably just exchange one bug for another.
Instead of passing 0 as notification ID, send some other number and trigger another notification with the same ID.
The existing one will be updated.
i want to create a notification that user can't close it by drag to left or right.
user must click o that to close notification
my code is :
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com"));
PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
Notification n = new Notification.Builder(getApplicationContext())
.setContentTitle("title")
.setContentText("text")
.setSmallIcon(R.drawable.ic_tt)
.setLargeIcon( BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.ic_tt))
.setContentIntent(pIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setOngoing(true).build();
n.flags |= Notification.FLAG_ONGOING_EVENT;
NotificationManager notificationManager =(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, n);
but it dosnot work
any idea ?
You need to use both functions to retain the notification,
nBuilder.setOngoing(true);
nBuilder.setAutoCancel(true);
It can be cancelled only when user taps on it.
Find setAutoCancel in this Managing Notifications.
Hope this helped!
I can use the following code to display a notification icon of missed calls, I hope to click the icon to open system Missed Calls UI, how can I do ? Thanks!
At present, I can open ui.CallerMain.class UI if I remove the comment.
BTW, in system Missed Calls UI, missed calls are listed in there.
private void ShowMissCallNotification(Context myContext,String myContentText) {
NotificationManager notificationManager = (NotificationManager) myContext.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(android.R.drawable.sym_call_missed,
myContext.getString(R.string.app_name),
System.currentTimeMillis());
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_NO_CLEAR;
CharSequence contentTitle= "Title";
CharSequence contentText =myContentText;
//Intent notificationIntent = new Intent(myContext, ui.CallerMain.class);
//PendingIntent contentItent = PendingIntent.getActivity(myContext, 0, notificationIntent, 0);
//notification.setLatestEventInfo(myContext, contentTitle, contentText,contentItent);
notificationManager.notify(NotificationID, notification);
}
Set a Pending Intent to the notification which will trigger the Call History.
First create an intent with Call Log
Intent resultIntent = new Intent();
resultIntent.setAction(Intent.ACTION_VIEW);
resultIntent.setType(CallLog.Calls.CONTENT_TYPE);
Then obtain the PendingIntent
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
Then set the PendingIntent to your notification builder
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon()
.setContentTitle()
.setContentText()
.setContentIntent(resultPendingIntent);
notificationManager.notify(id, builder.build());
Now clicking the notification will open the Call Log.
Update: The code commented out in your snippet will work if you create the Intent as mentioned above in this answer. But please be aware that the method by which you are creating notification has been deprecated. Use NotificationCompat class from the support library in future.
I have read many examples of how to create notification messages.
What i wanted to achieve, is because the notification will be executed by a widget, i would like
the notification intent when clicked to clear it self when the user clicks on it.I do not have an activity to return to.
The notification for my purposes will just plainly notify, nothing else.
So what would be the code of an intent that just clear/cancel itself.
The code below is an activity launched by a button(button code not included) the notification will be fired up by a background service.
CharSequence title = "Hello";
CharSequence message = "Hello, Android!";
final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
final Notification notification = new Notification(R.drawable.icon,"A New Message!",System.currentTimeMillis());
notification.defaults=Notification.FLAG_ONLY_ALERT_ONCE+Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(this, AndroidNotifications.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
notification.setLatestEventInfo(AndroidNotifications.this, title,message, pendingIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
Thanks
Check out FLAG_AUTO_CANCEL
Bit to be bitwise-ored into the flags field that should be set if the notification should be canceled when it is clicked by the user.
EDIT :
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Set the flags in notification.flags instead of notification.defaults.
Example:
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;
If you're using NotificationCompat.Builder (a part of android.support.v4) then simply call its object's method setAutoCancel
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setAutoCancel(true);
Some guys were reporting that setAutoCancel() did not work for them, so you may try this way as well
builder.build().flags |= Notification.FLAG_AUTO_CANCEL;
The only way I can see of doing this is to have your Notification's Intent point to a background Service. When this Service is launched, it would clear the given Notification using NotificationManager.cancel(int id). The Service would then stop itself. It's not pretty, and would not be easy to implement, but I can't find any other way of doing it.
/**
Post a notification to be shown in the status bar.
Obs.: You must save this values somewhere or even pass it as an extra through Intent to use it later
*/
notificationManager.notify(NOTIFICATION_ID, notification);
/**
Cancel a previously shown notification given the notification id you've saved before
*/
notificationmanager.cancel(NOTIFICATION_ID);
Using setContentIntent should solve your problem:
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));
For example:
NotificationCompat.Builder mBuilder= new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("title")
.setAutoCancel(true)
.setContentText("content")
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));
NotificationManager notificationManager= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mBuilder.build());
Often you might want to direct the user to the relevant content and so might replace 'new Intent()' with something else.