Months ago I wrote a code that my Notification worked as bellow :
NotificationManager NotiManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
String MyText = "NotificationText";
Notification mNotification = new Notification(R.mipmap.icon, MyText, System.currentTimeMillis() );
mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
mNotification.defaults |= Notification.DEFAULT_SOUND;
mNotification.defaults |= Notification.DEFAULT_VIBRATE;
String MyNotificationTitle = "AnyText";
String MyNotificationText = "HERE MORE TEXT";
Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse("https://www.google.com"));
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent StartIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
mNotification.setLatestEventInfo(context.getApplicationContext(), MyNotificationTitle, MyNotificationText, StartIntent);
NotiManager.notify(NOTIFY_ME_ID_LOGIN, mNotification);
But now, that I want to make an update it doesn't even let compile the APP because this line :
mNotification.setLatestEventInfo(context.getApplicationContext(), MyNotificationTitle, MyNotificationText, StartIntent);
Is there any way to change the setLatestEventInfo or other way to create a Notification?
Is there any way to change the setLatestEventInfo
You are welcome to lower your compileSdkVersion, though that will introduce its own set of issues.
or other way to create a Notification?
As Blackbelt notes in a comment, NotificationCompat.Builder has been around for ~4 years and is the recommended way to create Notification objects today:
private void raiseNotification(String mimeType, File output,
Exception e) {
NotificationCompat.Builder b=new NotificationCompat.Builder(this);
b.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL);
if (e == null) {
b.setContentTitle(getString(R.string.download_complete))
.setContentText(getString(R.string.fun))
.setSmallIcon(android.R.drawable.stat_sys_download_done)
.setTicker(getString(R.string.download_complete));
Intent outbound=new Intent(Intent.ACTION_VIEW);
outbound.setDataAndType(Uri.fromFile(output), mimeType);
b.setContentIntent(PendingIntent.getActivity(this, 0, outbound, 0));
}
else {
b.setContentTitle(getString(R.string.exception))
.setContentText(e.getMessage())
.setSmallIcon(android.R.drawable.stat_notify_error)
.setTicker(getString(R.string.exception));
}
NotificationManager mgr=
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
mgr.notify(NOTIFY_ID, b.build());
}
(from this sample project, which is in this directory of sample projects all showing how to display Notifications)
Related
I am creating a notification upon click I am asking it cancel a service. It just works fine below 4.4 (Kitkat). My app supports 2.2 (API 8 onwards)
On Kitkit (Nexus 5) The service isn't called at all. I am not where I am going wrong here? It just works fine even on version 4.3?
Here is what I have tried and working on every phone except Nexus 5(Kitkat)
createNotification("Click here to cancel notification!", "TestApp");
I am calling the above immediately after calling a particular service.
private void createNotification(String body,String title)
{
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
int unique_id = 007;
Intent nintent = new Intent(this,ServicetocancelAlarm.class);
PendingIntent pin = PendingIntent.getService(this,0, nintent, 0);
//set notify image
Notification n = new Notification(R.drawable.ic_launcher, body,java.lang.System.currentTimeMillis());
n.contentIntent = pin;
n.setLatestEventInfo(this, title, body, pin);
n.defaults = Notification.DEFAULT_ALL;
nm.notify(unique_id, n);
}
Can somebody help me out fixing this issue with KITKAT?
Update:
I tried the following:
Notification("Message", "This is Android Notification Message");
And Method
#SuppressWarnings("deprecation")
private void Notification(String notificationTitle, String notificationMessage)
{
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
android.app.Notification notification = new android.app.Notification(R.drawable.ic_launcher, "A New Message",
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, TransparentActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(MainActivity.this, notificationTitle, notificationMessage, pendingIntent);
notificationManager.notify(10001, notification);
}
This piece of code works fine on debug mode but if Export the apk and install it. It just doesn't work at all. It doesn't get to the new activity. I am not sure what is wrong here.
I solved the problem by trying the following code:
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= 16)
{
Context context = RepeatService.this;
Intent notificationIntent = new Intent(context, ServicetocancelAlarm.class);
PendingIntent contentIntent = PendingIntent.getService(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Resources res = context.getResources();
Notification.Builder builder = new Notification.Builder(context);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
.setTicker(res.getString(R.string.ticker))
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle(res.getString(R.string.app_name))
.setContentText(res.getString(R.string.cancelText));
Notification n = builder.build();
nm.notify(007, n);
}
Else part has the usual notification code that just works fine below API 16..
try it out!!
private void showNotification() {
NotificationManager mNotificationManager = (NotificationManager)getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
Notification notifyDetails = new Notification(R.drawable.alert_light_frame,"Alarma!",System.currentTimeMillis());
PendingIntent myIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(Intent.ACTION_VIEW, People.CONTENT_URI), 0);
notifyDetails.setLatestEventInfo(getApplicationContext(), "Alarma!", nombre, myIntent);
notifyDetails.flags |= Notification.FLAG_AUTO_CANCEL;
notifyDetails.icon |= HEREEEEEEEEEEE
mNotificationManager.notify(SIMPLE_NOTFICATION_ID++, notifyDetails);
Log.i(getClass().getSimpleName(),"Sucessfully Changed Time");
}
in "HEReeeeeeeeeeeee" i need put a route, for example "/mnt/sdcard/Pou/4_1362782019815.png"
Thx.
Try using Notification.Builder to build up your notifications as it makes things more convenient and additionally has a setLargeIcon() method that you can use to pass in any Bitmap you want (including one you load from the sdcard). There's also NotificationCompat.Builder in the support library if you need to target pre-Honeycomb
1) Get icon bitmap from sdcard:
File f = new File("/mnt/sdcard/photo.jpg");
Bitmap notificationIconBmp = BitmapFactory.decodeFile(f.getAbsolutePath());
2) Set bitmap for notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setLargeIcon(notificationIconBmp);
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify("direct_tag", NOTIF_ALERTA_ID, builder.build());
I'm having trouble getting Android's Big Text notifications to work as documented here: NotificationCompat.BigTextStyle. Here's the code I'm using to display notifications. I know all the data is coming back correctly because I can get it to display on the console and as the traditional content text and title. Am I doing something wrong? Do I need to define bigTestStyle somewhere else as well? Hopefully one of you has done this before and knows what could be missing. Thanks.
My code:
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
bigTextStyle.bigText(extras.getString("message"));
NotificationCompat.Builder bigTextNotification = new NotificationCompat.Builder(context)
.setContentTitle("My App")
.setContentIntent(pendingIntent)
.setContentText("Message:")
.setSound(soundUri)
.setTicker(extras.getString("message"))
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.splash)
.setAutoCancel(true)
.setVibrate(new long[] { 0, 100, 200, 300 })
.setStyle(bigTextStyle);
final int notificationId = (int) (System.currentTimeMillis() / 1000L);
NotificationManager thisone = (NotificationManager) getApplicationContext()
.getSystemService(Context.NOTIFICATION_SERVICE);
thisone.notify(notificationId, bigTextNotification.build());
From NotificationCompat.BigTextStyle documentation :
Helper class for generating large-format notifications that include a lot of text.
If the platform does not provide large-format notifications, this method has no effect. The user will always see the normal notification view.
Perhaps your platform doesn't support large-format notifications.
EDIT :
On the other hand, it's possible your problem is here :
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
bigTextStyle.bigText(extras.getString("message"));
You are not using the return value of bigText.
Try to change it to :
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
bigTextStyle = bigTextStyle.bigText(extras.getString("message"));
or to :
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle().bigText(extras.getString("message"));
EDIT2 :
Costom Notification Layout for older Android versions :
protected void onMessage(Context context, Intent intent) {
// Extract the payload from the message
Bundle extras = intent.getExtras();
if (extras != null) {
String message = (String) extras.get("message");
String title = (String) extras.get("title");
// add a notification to status bar
NotificationManager mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent myIntent = new Intent(this,MyActivity.class);
Notification notification = new Notification(R.drawable.notification_image, title, System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
contentView.setImageViewResource(R.id.image, R.drawable.notification_image);
contentView.setTextViewText(R.id.title, title);
contentView.setTextViewText(R.id.text, message);
notification.contentView = contentView;
notification.contentIntent = PendingIntent.getActivity(this.getBaseContext(), 0, myIntent, PendingIntent.FLAG_CANCEL_CURRENT);
mManager.notify(0, notification);
PowerManager pm = (PowerManager)
context.getSystemService(Context.POWER_SERVICE);
WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
wl.acquire(15000);
}
}
This is a two part issue. I have push notifications work with my app but I can't get a custom sound to work, 1. 2. I can't get the notifications to open the app when you click on it from the notification bar.
This is the code below. I've tried various things to import a custom sound. The sound in in my assets/sounds folder. Will that be carried over with the apk when installed and how to I access it? I'm using webview as well like to open the app to a specific link from the notification, can that be done also?
private void sendGCMIntent(final Context theContext, String theMessage)
{
int icon = R.drawable.noti_icon;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
theContext.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, theMessage, when);
String title = theContext.getString(R.string.app_name);
Intent notificationIntent = new Intent();
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(theContext, 0, notificationIntent, 0);
notification.setLatestEventInfo(theContext, title, theMessage, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
//String path = "file:///android_asset/sounds/soft.mp3";
//File file = new File(path);
//Log.i(TAG,"Sound exists : " + file.exists());
Uri soundUri = Uri.parse("file:///android_asset/sounds/soft.mp3");
if (soundUri != null) {
notification.sound = soundUri;
}
else {
notification.defaults |= Notification.DEFAULT_SOUND;
}
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
I reasearched this, and this code is what I got
The app crashes when I open the MainActivity
NotificationCompat.Builder builder =
new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Get Other APW Co. Apps on Play!")
.setContentText("Want more? All our apps are free!");
int mNotificationId = 001;
NotificationManager mNotifyMgr =
(NotificationManager) getSystemmService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, builder.build());
Try this, Give context name getSystemmService(Context.NOTIFICATION_SERVICE);.
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
myNotification = new Notification(R.drawable.ic_launcher,
"Notification!", System.currentTimeMillis());
Context context = getApplicationContext();
String notificationTitle = "App Name";
String notificationText = Msg;
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myBlog),
context, MainActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,
0, myIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
myNotification.defaults |= Notification.DEFAULT_SOUND;
myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
myNotification.setLatestEventInfo(context, notificationTitle,
notificationText, pendingIntent);
notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
This will work:
notificationManager =
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
myNotification = new Notification(R.drawable.ic_launcher,
"Notification!",
System.currentTimeMillis());
Context context = getApplicationContext();
String notificationTitle = "Get Other APW Co. Apps on Play!";
String notificationText = ""Want more? All our apps are free!"";
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(SOME_URL));
PendingIntent pendingIntent
= PendingIntent.getActivity(AndroidNotification.this,
0, myIntent,
Intent.FLAG_ACTIVITY_NEW_TASK);
myNotification.defaults |= Notification.DEFAULT_SOUND;
myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
myNotification.setLatestEventInfo(context,
notificationTitle,
notificationText,
pendingIntent);
notificationManager.notify(1, myNotification);
}});
You have to specify contentIntent, i.e. the PendingIntent that will be executed when the item is clicked. It's mandatory, and you haven't specified it which causes the error.
You can do this in the builder or on the Notification:
in builder:
builder.setContentIntent(contentIntent);
Notification n = builder.build();
on Notification:
Notification n = builder.build();
n.contentIntent = contentIntent;
Only after can you send it to the NotificationManager:
mNotifyMgr.notify(mNotificationId, n);
The exact contentIntent value depends on what you want to do. See reference here:
http://developer.android.com/reference/android/app/Notification.html#contentIntent
See working example here: https://github.com/nheid/unitedcoders-android/blob/master/src/com/unitedcoders/android/examples/download/DownloadProgress.java