I am trying to send a notification when the button is pressed, and when the user clicks the notification the activity ActPending should starts.
the below posted code did not either send a notification nor starts the ActPending.
please let me know what I did wrong and how to fix it.
code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_main);
mBtn_send = (Button) findViewById(R.id.btn_send_notification);
mBtn_send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
}
});
}
public void send() throws PendingIntent.CanceledException {
Intent intAct = new Intent(this, ActPending.class);
PendingIntent pending = PendingIntent.getActivity(this, 1, intAct, PendingIntent.FLAG_UPDATE_CURRENT);
//pending.send();
String title = getString(R.string.app_name);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setStyle(new NotificationCompat.BigTextStyle().bigText("BIG TEXT")).setContentText("CONTENTS").setContentTitle(title);
builder.setContentIntent(pending).setAutoCancel(true);
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(1, builder.build());
}
Paste this code:
Intent intent = new Intent(this, ActPending.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), new Random().nextInt(), intent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
//icon appears in device notification bar and right hand corner of notification
builder.setSmallIcon(R.mipmap.ic_launcher);
// Set the intent that will fire when the user taps the notification.
builder.setContentIntent(pendingIntent);
// Large icon appears on the left of the notification (it accepts Bitmap argument only)
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
// Content title, which appears in large type at the top of the notification
builder.setContentTitle("Notifications Title");
// Content text, which appears in smaller text below the title
builder.setContentText("Your notification content here.");
// The subtext, which appears under the text on newer devices.
// This will show-up in the devices with Android 4.2 and above only
builder.setSubText("Tap to view documentation about notifications.");
//Will clear the notification once it has beeen clicked
builder.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(5434, builder.build());
Related
When I receive the notification I would like it to automatically disappear and have to upload manually I, Thank you in advance.
Here I attach the code, in which I create the notification. What I want to happen is that when I receive the notification, I get off, I can see it on the screen, and after a few seconds I go up alone without having to interact with it.
public void notificacion(String label, String autor,String destino) {
NotificationCompat.Builder notifica = new
NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon((((BitmapDrawable) getResources()
.getDrawable(R.drawable.ic_launcher)).getBitmap()))
.setContentTitle("tittle")
.setContentText(label)
.setAutoCancel(true);
Intent intnot = new Intent(this, MainActivity.class);
intnot.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intnot.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intnot.putExtra("Destino", destino);
PendingIntent intnotpend = PendingIntent.getActivity(this, 0, intnot, PendingIntent.FLAG_UPDATE_CURRENT);
notifica.setContentIntent(intnotpend);
notifica.setFullScreenIntent(intnotpend, true);
notifica.setContentIntent(intnotpend);
}
NotificationManager notyman = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notyman.notify(10, notifica.build());
}
I am using AWS SNS to send push notification to both SNS topics and Device Endpoints. It shows notification if the app is not running but does not show notification while the app is running.
I will like to show notification even if the app is running because the notification will inform the app user of changes happening in another section of the app so they can go check it out when they are ready. Something like getting notification of a new Whatsapp message from another group chat while you are typing message to another person.
Below is the notification builder code. Any hint will be really appreciated. Thanks.
private void displayNotification(final String message) {
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
int requestID = (int) System.currentTimeMillis();
PendingIntent contentIntent = PendingIntent.getActivity(this, requestID, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Bitmap large_icon = BitmapFactory.decodeResource(this.getResources(),
R.mipmap.myImage);
// Display a notification with an icon, message as content, and default sound. It also
// opens the app when the notification is clicked.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setLargeIcon(large_icon)
.setContentTitle("My Subject Title")
.setContentText(message)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setContentIntent(contentIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(
Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}
I found the problem.
#Override
public void onMessageReceived(final String from, final Bundle data) {
String message = getMessage(data);
Log.d(LOG_TAG, "From: " + from);
Log.d(LOG_TAG, "Message: " + message);
// Display a notification in the notification center if the app is in the background.
// Otherwise, send a local broadcast to the app and let the app handle it.
displayNotification(message);
/* if (isForeground(this)) {
// broadcast notification
broadcast(from, data);
} else {
displayNotification(message);
} */
}
my displayNotification() function was not called because of the "if" conditional statement that will only display notification if the application is not in foreground. So now when onMessageReceived() gets the message it passes it directly to displayNotification() even if my application is currently running, which is what I want.
Use this below code to show notification message.
private static void generateNotification(Context context, final String message)
{
try {
NotificationManager mNotificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
//Define sound URI
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent notificationIntent = new Intent(context, DemoActivity.class);
notificationIntent.putExtra("message", message);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, RandomNumberGenerator.getRandom(),notificationIntent
/*new Intent(context, LuncherActivity.class)*/, 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context);
mBuilder.setSmallIcon(R.drawable.icon_small);
mBuilder.setContentTitle("YOUR-APP-NAME");
// mBuilder.setStyle(new NotificationCompat.BigTextStyle()
// mBuilder.bigText(msg);
mBuilder.setContentText(message);
mBuilder.setAutoCancel(true);
mBuilder.setContentIntent(contentIntent);
mBuilder.setSound(soundUri);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
Create a random number genetor class like below
public class RandomNumberGenerator {
public static int getRandom()
{
int random_number=0;
Random random = new Random();
random_number= random.nextInt(9999 - 1000) + 1000;
return random_number;
}
}
Try it and let me know.its working in my case.
I am developing a flashLight app and I faced a problem in notification which is : When I press the notification text I want to turn of flash and close the whole app , How can I modified the notification method to do that ??
I tried to make anthor activity and put turn of and close app methods in it, but it does not work .
please help and thanks in advance .
this is my notification method
public void getNotification()
{
Intent intent = new Intent();
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent , PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setContentIntent(pIntent)
.setContentTitle(res.getString(R.string.notification_title))
.setContentText(res.getString(R.string.notification_text))
.setSmallIcon(R.drawable.icon)
.setAutoCancel(true)
.setTicker(getString(R.string.notification_ticker_msg));
// Build the notification:
Notification notification = builder.build();
// Get the notification manager:
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
// Publish the notification:
final int notificationId = 0;
notificationManager.notify(notificationId, notification);
}
If you use the same Activity, then the onCreate method will be called again. You can send one extra with your Intent that indicates it is an Intent generated from the click of notification. In your Activity onCreate, check for this extra and call finish() if it is present.
public void getNotification() {
Intent intent = new Intent(this, FlashActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("origin", "notification");
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent , PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setContentIntent(pIntent)
.setContentTitle(res.getString(R.string.notification_title))
.setContentText(res.getString(R.string.notification_text))
.setSmallIcon(R.drawable.icon)
.setAutoCancel(true)
.setTicker(getString(R.string.notification_ticker_msg));
// Build the notification:
Notification notification = builder.build();
// Get the notification manager:
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
// Publish the notification:
final int notificationId = 0;
notificationManager.notify(notificationId, notification);
}
And in your onCreate method in FlashActivity check the extra.
#Override
public void onCreate(Bundle savedInstanceState) {
...
if("notification".equals(getIntent().getStringExtra("origin"))) {
finish();
}
}
I believe you can use finish() on your Activity when the notification ils pressed.
EDIT: How to close any activity of my application by clicking on a notification?
You want to use PendingIntent.
Intent resultIntent = new Intent(this, MainActivity.class);
resultIntent.putExtra("FINISH",true);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
builder.setContentIntent(resultPendingIntent);
// Build the notification:
Notification notification = builder.build();
//rest of your methods to show notification
In your MainActivity update depending on your code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onNewIntent(getIntent());
}
#Override
public void onNewIntent(Intent intent){
setContentView(R.layout.activity_main);
Bundle extras = intent.getExtras();
if(extras != null){
if(extras.containsKey("FINISH"))
{
finish();
}
}
}
I am trying to create a notification for android device. This is the code written on the mobile. It shows the notification on the mobile phone but does not show up in android wear. Am I missing out on something?
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Log.i("Tag","Inside Self destruct");
//Code for notification begins here
PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(context, ShowNotificationDetailActivity.class), 0);
Resources r = getResources();
String greetingIdText = r.getString(R.string.notification_text);
Notification notification = new NotificationCompat.Builder(context)
.setTicker(r.getString(R.string.notification_title))
.setSmallIcon(android.R.drawable.ic_menu_report_image)
.setContentTitle(r.getString(R.string.notification_title))
.setContentText(greetingOne.getContent())
.setContentIntent(pi)
.setAutoCancel(true)
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(0, notification);
}
});
I am working on this app. what i want to do is display the icon of my activity in notification bar on a button click. i want it to stay there permanently until some other event is done. currently i dound this code but its giving a lot of errors, kindly tell me an easy way to do this:
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(icon.isChecked()) {
CharSequence text = getText(R.string.local_service_started);
// Set the icon, scrolling text and timestamp
Notification notification = new Notification(R.drawable.ic_launcher, "Track Your Life", System.currentTimeMillis());
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(), 0);
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, getText(R.string.local_service_label), text, contentIntent);
// Send the notification.
mNM.notify(NOTIFICATION, notification);
}
}
});
Simple code for create Notification with icon
Intent intentSetDefault = new Intent(mContext, ConversationListActivity.class);
PendingIntent piSetDeafult = PendingIntent.getActivity(mContext, 0, intentSetDefault,
PendingIntent.FLAG_UPDATE_CURRENT);
// create pending intent for action discard
Intent intentdiscard = new Intent(mContext, NotificationReceiver.class).setType(CODE_DISCARD);
PendingIntent piDiscard = PendingIntent.getBroadcast(mContext, 0, intentdiscard,
PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder mBuilder =
new Notification.Builder(mContext)
.setTicker(title)
.setSmallIcon(R.drawable.ic_stat_notify_incoming_msg)
.setContentTitle(title)
.setContentText(text)
.setLargeIcon(mAppBitmap)
.setContentInfo(Integer.toString(smsCount))
.setPriority(Notification.PRIORITY_MAX)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(false);
mBuilder.setContentIntent(piSetDeafult);
Notification notification = mBuilder.build();
notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
mNotifyMgr.notify(NOTIFICATION_SET_DEFAULT, notification);