Android - Notification isn't appeared - java

I'm trying to create Notification using AppCompat inside Fragment.
I create see nothing.
Code:
private void createNotification()
{
Context context = getActivity().getApplicationContext();
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ymk_ya_logo)
.setAutoCancel(false)
.setContentTitle("notify")
.setContentText("test");
Notification notification = builder.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(777, notification);
}

Try changing your last bit to this instead:
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());

Related

How resume app after click notification with Java Android?

I create a notification and I want to resume app after click notification. Now when I click notification , a notification dissapear , I want to resume my app (when is minimalize).
public static void getSynchronizeNotification(Context context, DataResponse dataResponse) {
Bitmap Largeicon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_SYNCHRONIZE_ID);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
KeyguardManager km = (KeyguardManager) context.getSystemService(KEYGUARD_SERVICE);
final KeyguardManager.KeyguardLock kl = km.newKeyguardLock("IN");
kl.disableKeyguard();
PowerManager pm = (PowerManager) context.getSystemService(POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.FULL_WAKE_LOCK, "SMOK Komunal");
wl.acquire();
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setCategory(Notification.CATEGORY_PROMO)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Zmiany po synchronizacji...")
.setStyle(new NotificationCompat.BigTextStyle().bigText(buildNotificationAfterSynchronizeText(dataResponse)))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setLargeIcon(Largeicon)
.setContentIntent(pendingIntent)
.setVibrate(vibratePattern)
.setLights(Color.GREEN, 2000, 2000)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
notificationManager.notify(0, notificationBuilder.build());
// wl.release();
}
Edit this line
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
I do this and it works :
In MainAcitivity I have :
public static Context currentContext;
Next in all activity in onCreate and onResume and do this :
MainActivity.currentContext = this;
And next when I build a notification I do this :
Intent intent = new Intent(context, MainActivity.currentContext.getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
This works;
<activity android:name=".YourActivity" android:launchMode="singleTop">

I am trying to generate notification on app,but i got error cannot resolve setLatestEventInfo() method

private void Notify(String notificationTitle, String notificationMessage){
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
#SuppressWarnings("deprecation")
Notification notification = new Notification(R.drawable.logob,"New Message", System.currentTimeMillis());
Intent notificationIntent = new Intent(this,NotificationView.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(foradmin.this, notificationTitle,notificationMessage, pendingIntent);
notificationManager.notify(9999, notification);
}
Trying to generate notification on app,but i got error cannot resolve setLatestEventInfo() method.
try like this. it works perfectly.Hope this helps.Thanks
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 1, intent, 0);
Notification.Builder builder = new Notification.Builder(MainActivity.this);
builder.setAutoCancel(false);
builder.setTicker("this is ticker text");
builder.setContentTitle("WhatsApp Notification");
builder.setContentText("You have a new message");
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentIntent(pendingIntent);
builder.setOngoing(true);
builder.setSubText("This is subtext..."); //API level 16
builder.setNumber(100);
builder.build();
myNotication = builder.getNotification();
manager.notify(11, myNotication);

Android click notification to open web page

I am trying to open a web page when a notification is clicked:
public void shownNotification(String title, String message){
Intent notifyIntent = new Intent(this, MainActivity.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
notifyIntent.setData(Uri.parse("htp://www.google.com"));
PendingIntent pendingItent = PendingIntent.getActivities(this, 0, new Intent[] {notifyIntent }, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(this)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setContentIntent(pendingItent)
.build();
notification.defaults |= Notification.DEFAULT_SOUND;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);
}
I get the notification with out a problem but when I click it, it just bring me to the application.
Change your notifyIntent to:
Intent notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.yoururl.com"));
Then your code will be something like:
public void shownNotification(String title, String message){
Intent notifyIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.yoururl.com"));
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingItent = PendingIntent.getActivities(this, 0, new Intent[] {notifyIntent }, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(this)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setContentIntent(pendingItent)
.build();
notification.defaults |= Notification.DEFAULT_SOUND;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);
}

Clearing Notification from invoked Activity

I use this code to show a notification:
public static void ShowCustomNotification(Context context,String notificationTitle, String notificationMessage)
{
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// intent triggered, you can add other intent for other actions
Intent intent = new Intent(context, ResultActivity.class);
intent.putExtra("title", notificationTitle);
intent.putExtra("body", notificationMessage);
intent.setFlags(PendingIntent.FLAG_UPDATE_CURRENT
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
Notification mNotification = new NotificationCompat.Builder(context)
.setContentTitle(notificationTitle)
.setSmallIcon(R.drawable.office_girl)
.setContentIntent(pIntent)
.setSound(soundUri)
.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mNotification);
}
When the user clicks the Notification the activity ResultActivity.class is invoked and everything is fine.
My question is how can i clear the notification on the status bar when a user click on a "Close" button from inside ResultActivity? in case the user click "Back" i dont want to clear the notification.
Thanks.
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(YOUR_NOTIFICATION_ID);

Notification onClick

I have successfully created a notification thanks to my other question, using NotificationCompat. I want to now just change what the notification does onClick. I'd really just like to have an alert dialog pop up (I've seen some app's do it) but everytime I click it, I just have my activity show up. Any ideas?
The notification code:
Intent notificationIntent = new Intent(ctx, YourClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(ctx,
YOUR_PI_REQ_CODE, notificationIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager nm = (NotificationManager) ctx
.getSystemService(Context.NOTIFICATION_SERVICE);
Resources res = ctx.getResources();
Notification.Builder builder = new Notification.Builder(ctx);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.some_img)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.some_big_img))
.setTicker(res.getString(R.string.your_ticker))
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle(res.getString(R.string.your_notif_title))
.setContentText(res.getString(R.string.your_notif_text));
Notification n = builder.build();
nm.notify(YOUR_NOTIF_ID, n);
You can't have a normal dialog without an activity. There are several possible workarounds though including styling the activity like a dialog and making the activity itself invisible and launching a dialog from it immediately.
You can probably use Remote Views to show a dialog without going to your app process.
you can set
Intent intent = new Intent(context, ReserveStatusActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
intent = new Intent(String.valueOf(PushActivity.class));
intent.putExtra("message", MESSAGE);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(PushActivity.class);
stackBuilder.addNextIntent(intent);
// PendingIntent pendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.BigPictureStyle notiStyle = new
NotificationCompat.BigPictureStyle();
android.app.Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(en_title)
.setContentText(en_alert)
.setAutoCancel(true)
.setNumber(++numMessages)
.setStyle(notiStyle)
//.setContentIntent(resultPendingIntent)
.setContentIntent(pendingIntent)
.build();
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationManager.notify(1000, notification);

Categories

Resources