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

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());
}
}

Related

Update Notification with a Button click

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.

When user taps on the push notification message, my app launches. How to make the app display the push message in full?

Good day!
I have successfully sent push notification message to my app on android, and when I tap on the message, it launches my app. May I ask how do I pass intent/bundle to MainActivity from the push notification so that when the app is launched, it can display the push notification message in full within the app? Thank you very much!
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmMessageHandler will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmMessageHandler.class.getName());
showNotification(context, intent);
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
private void showNotification(Context context, Intent intent) {
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, MainActivity.class), 0);
String title = intent.getExtras().getString("nTitle");
String message = intent.getExtras().getString("nMessage");
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context);
Notification notification = mBuilder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.face)
.setColor(context.getResources().getColor(R.color.wallet_holo_blue_light))
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.fuckya))
.setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentText(message).build();
mBuilder.setContentIntent(contentIntent);
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
}
Regards,
Lorkh
Replace to:
Intent mainActivityIntent = new Intent(context, MainActivity.class);
String title = intent.getExtras().getString("nTitle");
String message = intent.getExtras().getString("nMessage");
mainActivityIntent.putExtra("nTitle", title);
mainActivityIntent.putExtra("nMessage",message);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, mainActivityIntent), 0);
And retrieve this values inside your MainActivity via getIntent();

Cancel notification not working

I want to cancel/delete the notification after I click the addAction.
However it's not working. The notification is still there after the click.
I'm pretty sure this worked in an other project.
Can anyone see a stupid error I made, why its not working?
Actual code:
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent arg1) {
showNotification(context);
}
private void showNotification(Context context){
String onderwerp = ("Medicatietijd");
String name = ("Het is tijd om je medicitie in te nemen.");
// Geluid notificatie
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// Notificatie trigger
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, Test.class), 0);
// De notificatie
Notification mNotification = new Notification.Builder(context)
.setContentTitle(onderwerp)
.setContentText(name)
.setSmallIcon(R.drawable.ninja)
.setSound(soundUri)
.addAction(R.drawable.ja, "Ja, ik heb ze ingenomen.", contentIntent)
.setAutoCancel(true)
.build();
NotificationManager notificationManager
= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotification.vibrate = new long[]{100, 200, 100, 500};
mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, mNotification);
}
Solution:
In test activity OnCreate added this:
NotificationManager notificationManager
= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(0);
If you decided to use Test activity to receive the intent of your addAction call, then you must cancel notification when you receive the intent in the activity.
I also recommend that you add requestCode for the intent.
Here is the code :
to set the requestCode modify this :
static final int REQ_CODE = 101; // some number
// Notificatie trigger
PendingIntent contentIntent = PendingIntent.getActivity(context, REQ_CODE,
new Intent(context, Test.class), 0);
to Handle intent in activity and dismiss the notification, in Test activity class :
#Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
if (requestCode == REQ_CODE) {
// dismiss notification
notificationManager.cancel(0);
// handle your action
// ...
}
}
Hope that helps

Android - Vibrate without displaying new Activity?

I have the following code in an android app. What it currently does is, at the specified time passed with the Calendar when variable, it opens up the RunningActivity (which is blank), vibrates, and sends a notification. Even if I've pressed the home button and it's running in the background, it starts a new blank RunningActivity and vibrates and sends a notification. I'm trying to figure out how to do all the stuff in the RunningActivity (Vibrate and send a notification) without opening up the blank RunningActivity, allowing the application to stay in the background.
I do NOT need help with actually calling the notification or vibration. I just need to know how to run the actions in the RunningActivity onCreate at a specific time witout opening/showing the RunningActivity. As seen below, I setup a PendingIntent with an AlarmManagager, the issue is that it is launching an Activity and showing it when all I want it to do is vibrate/send a notification.
public void startAlarm(Activity activity, Calendar when){
currentTimerHour = when.get(Calendar.HOUR);
currentTimerMin = when.get(Calendar.MINUTE);
Intent intent = new Intent(activity, RunningActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(activity, 12345, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager)activity.getSystemService(Activity.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), pendingIntent);
}
RunningActivity Class:
public class RunningActivity extends Activity {
#Override
public void onCreate(Bundle bundle){
super.onCreate(bundle);
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(500);
NotificationCompat.Builder mBuilder;
mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!");
Intent resultIntent = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
}
}
To send Notification From Background:
public void createNotification(Context context) {
// Prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(context,Myexample.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
Notification noti = new Notification.Builder(context)
.setContentTitle("My Title")
.setContentText("My message.")
.setSmallIcon(R.drawable.app_icon)
.setContentIntent(pIntent).build();
#SuppressWarnings("static-access")
NotificationManager notificationManager =
(NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
// Hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
noti.flags |= Notification.FLAG_SHOW_LIGHTS;
notificationManager.notify(0, noti);
}
You can use service. Add your vibrator class on Service and call it when you need need it. You can also create a method on your class and call it when you need it.
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(500);

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.

Categories