Update Notification with a Button click - java

I just a newbie of Android, while I programming I had the problem that is about the Notification.
I need your help to process updating notification.
The context of this like when you are playing the game and you had a notification about another game (the second game is running in the background). Then you have a new notification of the second game which has the same ID of the previous notification.
This is my declaration:
I used NotificationManagerclass to create a Notification.
private NotificationManager manager;
private int notiId = 6789; // Each notification will be managed by an ID
private int numMsg = 0;
This is the function clickToSend button:
public void clickToSend(View view) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
// Setting Notification Properties
builder.setContentTitle("New Message");
builder.setContentText("Notification Demo: Message has received");
builder.setTicker("Message Alert");
builder.setSmallIcon(R.drawable.ic_action_unread);
builder.setNumber(++numMsg);
Intent intent = new Intent(this, NotificationDetailActivity.class);
TaskStackBuilder stack = TaskStackBuilder.create(this);
stack.addNextIntent(intent);
PendingIntent pendingIntent = stack.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(notiId, builder.build());
}
I think code of function clickToUpdate that will process like the clickToSend function.
Thanks for your help!
My language is not good. I'm sorry for the inconvenience.

Related

How to display a popup notification on user time selection in android?

I am new to Android.I have to display the popup notification on user time selection using time picker when the notification toggle button is on and also when the app is closed it shows the notification. Can anyone help?
public class Utils {
public static NotificationManager mManager;
#SuppressWarnings("static-access")
public static void generateNotification(Context context){
android.support.v7.app.NotificationCompat.Builder nb= new android.support.v7.app.NotificationCompat.Builder(context);
nb.setContentTitle("Some text here");
nb.setContentText("Some text here");
nb.setTicker("Take a look");
nb.setSmallIcon(R.mipmap.ic_launcher);
nb.setAutoCancel(true);
//get the bitmap to show in notification bar
Bitmap bitmap_image = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon);
android.support.v7.app.NotificationCompat.BigPictureStyle s = new android.support.v7.app.NotificationCompat.BigPictureStyle().bigPicture(bitmap_image);
s.setSummaryText("some text here");
nb.setPriority(Notification.PRIORITY_MAX);
nb .setDefaults(NotificationCompat.DEFAULT_VIBRATE);
nb.setStyle(s);
Intent resultIntent = new Intent(context, MainActivity.class);
TaskStackBuilder TSB = TaskStackBuilder.create(context);
TSB.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
TSB.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
TSB.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
nb.setContentIntent(resultPendingIntent);
nb.setAutoCancel(true);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(11221, nb.build());
}
}

android notification banner not displaying

for some reason when I make a notification appear on my app in will vibrate and make noise but no banner appears across the top of the phone. Is there a specific command I need to make it do this?
public Notification getNotification(String message) {
Intent intent = new Intent(serviceContext, NotificationGenerator.class); //not sure how
// this class i pass matters
PendingIntent pi = PendingIntent.getActivity(serviceContext, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(serviceContext)
.setContentTitle(message)
.setContentText(message)
.setSmallIcon(R.drawable.dominos_icon)
.setContentIntent(pi);
Notification n = builder.build();
n.defaults = Notification.DEFAULT_ALL;
return n;
}
Your code will work fine once you will add the lines below:
NotificationManager n_mngr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
n_mngr.notify(NOTIF_ID, n);

Notification after a time

I want to add a Notificationmanager which should start after some milliseconds.
Everything is running, but still I don't knoe how to add the time that the notification comes after some weeks for example?
public void test(){
int icon = R.drawable.ic_launcher;
CharSequence ticker ="ticker";
long showAt =System.currentTimeMillis();
Notification notification = new Notification(icon, ticker, showAt);activity/intent
CharSequence notificationTitle = "test";
CharSequence notificationMessage = "Test1";
Intent intent = new Intent(this, Activity.class);
PendingIntent objPendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Context ctx = getApplicationContext();
notification.setLatestEventInfo(ctx, notificationTitle, notificationMessage, objPendingIntent);
final int notificationIdentifier = 101; //an unique number set by developer to identify a notification, using this notification can be updated/replaced
NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationIdentifier, notification);
// ...
}
You need to use Alarm manager with notification for repeating your notification after a regular time interval. Check AlarmManagerNotificationManagerTutorial , Android AlarmManager tutorial and Multiple Notifications Using AlarmManager . There are many tutorials out there. Hope this helps.

I Have a notification in the status bar code, but AIDE is marking two things as wrong

I need to make a Status Bar Notification and I got this code from research, but it doesn't seem to be working out when I placed the code into MainActivity.java with AIDE
NotificationManager
mNotificationManager = (NotificationManager)
getSytemService(Context.N);
Notification notification = new notification(R.drawble.ic_launcher,
"Notification Test", System.currentT;
Context context = getApplicationContext();
CharSequence contentTitle = "My notification Title";
CharSequence contentText ="This is the message";
Intent notificationIntent = new Intent(this, MainActivity.class);
The errors seem to be at Line 3 with the (Context.N) where the N has the error message: Unknown member 'N' of 'android.content.Context'. The other error message is on the fifth line where it reads System.currentT with the error message of Unknown member 'currentT' of 'java.lang.Sytem'.
Context.N should be Context.NOTIFICATION_SERVICE.
System.currentT should be System.currentTimeMillis().
It seems that you are copying the code from somewhere but got the code truncated?
Here's a sample method for generating notifications.
This is based on Android's doc: Building a Notification. For more details (e.g. how to display a progress bar or an extended view), please read the docs.
private static void generateNotification(Context context, String messageTitle, String messageText)
{
// Get notification manager.
final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Setup notification builder.
final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(messageTitle)
.setContentText(messageText)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL);
// Create intent.
final Intent resultIntent = new Intent(context, MainActivity.class);
// Setup task stack builder.
final TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
taskStackBuilder.addParentStack(MainActivity.class);
taskStackBuilder.addNextIntent(resultIntent);
// Create pending intent.
final PendingIntent resultPendingIntent = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
// Update notification builder.
notificationBuilder.setContentIntent(resultPendingIntent);
// Post notification.
notificationManager.notify(0, notificationBuilder.build());
}
The NotificationCompat.Builder class requires Android's version 4 Support Library.

notification opening a new window no matter what in Java Android

I want to launch a notification. When I click on it, it opens a NEW window of the app.
Here's my code:
public class Noficitation extends Activity {
NotificationManager nm;
static final int uniqueID = 1394885;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent= new Intent (Intent.ACTION_MAIN);
intent.setClass(getApplicationContext(), SchoolBlichActivity.class);
PendingIntent pi=PendingIntent.getActivity(this, 0, intent, 0);
String body = " body";
String title = "title!";
Notification n =new Notification(R.drawable.table, body, System.currentTimeMillis());
n.setLatestEventInfo(this, title, body, pi);
n.defaults = Notification.DEFAULT_ALL;
n.flags = Notification.FLAG_AUTO_CANCEL;
nm.notify(uniqueID,n);
finish();
}
by the way, if i add nm.cancel(uniqueID) before the finish(), it creates the notification and immediately deletes it...
Thanks for the help :D
You might want to just add a notification in the notification bar, and when the user clicks it, it will launch the actual Activity. This way the user won't be interrupted in whatever he's doing.
Create the status bar notification like this:
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.notification_icon, "Hello", System.currentTimeMillis());
Intent notificationIntent = new Intent(this, myclass.class);
notification.setLatestEventInfo(getApplicationContext(), "My notification", "Hello world!", notificationIntent, PendingIntent.getActivity(this, 0, notificationIntent, 0));
mNotificationManager.notify(1, notification);
http://developer.android.com/guide/topics/ui/notifiers/notifications.html
Are you just trying to open a notification window in a current activity? Because if you are I dont think you need to launch it with an intent. You normally only use intents to launch new services or activities in your app unless youve built a custom view and activity/service which is to take place within the notification box. I see you have it set up in its own class which is fine but I think the way your doing it by default would open an entire new view.
If you need to launch a notification during a process or something like a button click you dont need to have the intent there.....or at least I never did :) What exactly are you trying to achieve with the notification.

Categories