Dealing with Notifications on API 10 in Android - android

The code i'm putting in onCreate() is the following:
if(Build.VERSION.SDK_INT >= 11)
notifApiGT10();
else
notifApiLT10();
Where,
#SuppressWarnings("deprecation")
#SuppressLint("NewApi")
private void notifApiGT10() {
// TODO Auto-generated method stub
Notification.Builder builder ;
NotificationManager notifier;
builder = new Notification.Builder(this);
builder.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.setOngoing(true).setContentTitle("my Title").setContentText("my displayed text");
notifier = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
if(Build.VERSION.SDK_INT>=11 && Build.VERSION.SDK_INT<=15)
notifier.notify(1, builder.getNotification());
else if (Build.VERSION.SDK_INT > 15)
notifier.notify(1, builder.build());
}
private void notifApiLT10()
{
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setContentText("is actually in use")
.setContentTitle("my Title")
.setOngoing(true)
.setTicker("my ticker")
.setSmallIcon(R.drawable.ic_launcher);
Notification notif = builder.getNotification();
NotificationManager mN = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
mN.notify(1, notif);
}
The above code is not working on API 10. My device is running Gingerbread and it's not working on it.
I wonder why...Any experts?
SOLUTION:
Notification notification = new Notification(R.drawable.ic_launcher, "my ticker", System.currentTimeMillis());
notification.flags = Notification.FLAG_ONGOING_EVENT;
Context context = getApplicationContext();
Intent i = new Intent(this,Main.class);
PendingIntent pd = PendingIntent.getActivity(this, 1, null, Intent.FLAG_ACTIVITY_NEW_TASK);
notification.setLatestEventInfo(context, "my title", "my text", pendingintent);
NotificationManager mN = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
mN.notify(1,notification);
The above solution solved my problem,
even if you don't want to run anything on click, you should put the pendingintent inside the method setLatestEventInfo, but in pd as you noticed, in the 3rd field, i have set the intent to null

Have you tried using NotificationCompat.Builder.build()? NotificationCompat.Builder.getNotification() is deprecated.
FYI, last time I used NotificationCompat.Builder, setNumber() did not work properly and I ended up constructing Notification directly on older devices.
EDIT: Try using Notification class directly on older devices because NotificationCompatImplBase (API <= 10) uses only 4 fields from the builder no matter how many are set.

Related

permanent notification does not show

I submitted my app at the end of 2017 in the play store and it worked.
Then it was built in Eclipse.
Now I run it in Android Studio and the same code doesn't show any notification.
It is a permanent notification which is present after I call the app's finish() method. (why I do this: see at the bottom if you want to know).
So I looked at all the examples and NONE of the work. No single notification is shown.
So I start an activity and in the activity I show the notification. Then I call finish().
This is my code:
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
intent, 0);
CharSequence connectedPart = getResources().getString(R.string.app_name);
String melding = "";
melding = getResources().getString(R.string.screentimeoutsetto) + " " + newTimeString + ".";
NotificationManager notificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification.Builder(this)
.setContentTitle(connectedPart)
.setTicker(connectedPart)
.setContentText(melding)
.setSmallIcon(R.drawable.ic_stat_name)
.setContentIntent(pendingIntent)
.setOngoing(true)
.build();
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_NO_CLEAR;
notificationManager.notify(2, notification);
dependencies:
dependencies{
implementation "com.android.support:support-compat:26.1.0"
}
Why I do this:
My app does 1 thing: toggle the screentimeout. So when you start it it sets the screentimeout to 30 minutes (starts, sets a notification, then finish()). Start again, it removes the notification and restore the original value for the screen timeout.
Starting with Android Oreo, all notifications require a notification channel, otherwise the notification wont post and an error will appear in your logcat. Here's an example of how you can do it, lifted from one of my apps:
private static void createNotificationChannel(Context ctx) {
if(SDK_INT < O) return;
final NotificationManager mgr = ctx.getSystemService(NotificationManager.class);
if(mgr == null) return;
final String name = ctx.getString(R.string.channel_name);
if(mgr.getNotificationChannel(name) == null) {
final NotificationChannel channel =
new NotificationChannel(CHANNEL_ID, name, IMPORTANCE_DEFAULT);
mgr.createNotificationChannel(channel);
}
}
//Usage...
createNotificationChannel(context);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID);
builder.setSmallIcon(R.drawable.ic_alarm_white_24dp);
//etc...
manager.notify(id, builder.build());
Now from Android 8.0 and onward you need to create notification channel before showing notifications. Here is the Docs link
https://developer.android.com/training/notify-user/channels

Android Custom Notification with RemoteView is not showing properly blow Nougat OS

I m trying to create a custom notification using RemoteView. It is showing properly in Nougat and above OS version devices but has an issue with marshmallow and below OS version devices.
Here is default Big icon (Left side inside the Red circle) and default time (In Right side red circle) is not hiding after Remote view.
Please suggest me how I can come out of this problem.
private void sendNotification() {
Long id = System.currentTimeMillis();
int notifyId = id.intValue();
RemoteViews expandedView = new RemoteViews(getPackageName(), R.layout.view_expanded_notification);
expandedView.setTextViewText(R.id.timestamp, DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME));
expandedView.setTextViewText(R.id.notification_message, mEditText.getText());
// adding action to left button
Intent leftIntent = new Intent(this, NotificationIntentService.class);
leftIntent.setAction("left");
expandedView.setOnClickPendingIntent(R.id.left_button, PendingIntent.getService(this, 0, leftIntent, PendingIntent.FLAG_UPDATE_CURRENT));
// adding action to right button
Intent rightIntent = new Intent(this, NotificationIntentService.class);
rightIntent.setAction("right");
expandedView.setOnClickPendingIntent(R.id.right_button, PendingIntent.getService(this, 1, rightIntent, PendingIntent.FLAG_UPDATE_CURRENT));
RemoteViews collapsedView = new RemoteViews(getPackageName(), R.layout.view_collapsed_notification);
collapsedView.setTextViewText(R.id.timestamp, DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME));
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "imp")
// these are the three things a NotificationCompat.Builder object requires at a minimum
.setSmallIcon(R.drawable.ic_pawprint)
.setContentTitle(NOTIFICATION_TITLE)
.setContentText(CONTENT_TEXT)
// notification will be dismissed when tapped
.setAutoCancel(true)
// tapping notification will open MainActivity
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0))
// setting the custom collapsed and expanded views
.setCustomContentView(collapsedView)
.setCustomBigContentView(expandedView)
// setting style to DecoratedCustomViewStyle() is necessary for custom views to display
.setStyle(new NotificationCompat.DecoratedCustomViewStyle());
// retrieves android.app.NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel("imp", "Test", importance);
channel.setDescription("For testing");
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
notificationManager.notify(notifyId, builder.build());
} else {
NotificationManager notificationManager = (android.app.NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}
}
This is properly shown in Nougat and above devices.
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, id);
builder.setSmallIcon(R.drawable.icon_notification)
.setAutoCancel(true)
.setCustomBigContentView(remoteExpandedViews)
.setCustomContentView(remoteCollapsedViews)
.setContentIntent(pendingIntent)
**.setPriority(Notification.PRIORITY_MAX)**
.setVibrate(new long[]{0});
set Priority to PRIORITY_MAX using .setPriority(Notification.PRIORITY_MAX) in builder

BigContentView does not seem to be updated

I am currently working on an android app. I need to notify a user that they have arrived at a place in the normal notification and when the notification is expanded it shows extra info. The usual :)
I am using 2 different remoteViews, one to set the contentView and one to set the bigContentView.
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// build notification
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(notificationPendingIntent)
.setColor(Color.CYAN)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setAutoCancel(true)
.setContentTitle("Content Title")
.setContentText("Content Text");
int NOTIFICATION_ID = 1;
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.remoteview_notification);
rv.setImageViewResource(R.id.remoteview_notification_icon, R.mipmap.ic_launcher);
rv.setTextViewText(R.id.remoteview_notification_headline, "Headline");
rv.setTextViewText(R.id.remoteview_notification_short_message, notificationContent.getHeading());
Notification notification = mBuilder.build();
if (android.os.Build.VERSION.SDK_INT >= 16) {
notification.contentView = rv; //Alternative to .setContent(rv);
}
mNotificationManager.notify(NOTIFICATION_ID, notification);
RemoteViews rvBig = new RemoteViews(context.getPackageName(), R.layout.remoteview_big_notification);
rvBig.setImageViewResource(R.id.remoteview_big_notification_icon, R.mipmap.ic_launcher);
rvBig.setTextViewText(R.id.remoteview_big_notification_headline, notificationContent.getHeading());
rvBig.setTextViewText(R.id.remoteview_big_notification_full_message, notificationContent.getBodyText());
if (android.os.Build.VERSION.SDK_INT >= 16) {
notification.bigContentView = rvBig;
}
mNotificationManager.notify(NOTIFICATION_ID, notification);
The problem is my expanded notification does not show up when I drag down? Any help will be appreciated. It feels like it is just something small that is not yet set/ set correctly?
I believe that my intentId might not be unique throughout my app. I have seen people use the current system to ensure uniqueness.
NOTIFICATION_ID = system.currenttimemillis();
PendingIntent pendingIntent = PendingIntent.getActivity(this, NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationManager.notify(NOTIFICATION_ID, builder.build());

pending intent not working in notificationbar

i have this code
NotificationCompat.Builder mBuilder= new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("Ticker")
.setContentTitle(getResources().getString(R.string.app_name))
.setAutoCancel(true)
.setContentText("Text")
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this,myActivity.class), 0));
NotificationManager notificationManager= (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, mBuilder.build());
i run this code on htc one mini with android 4.3 and it is working correctly
but when i run this code on nexus 5(android:4.4.2 runtime:art) and when i clicked on the notification myActivity didn't start and only remove my notification(AutoCancle)
any ideas?thanks in advance
Try this :
private void addNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
String appname = context.getResources().getString(R.string.app_name);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification;
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, myactivity.class), 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context);
notification = builder.setContentIntent(contentIntent)
.setSmallIcon(icon).setTicker(appname).setWhen(0)
.setAutoCancel(true).setContentTitle(appname)
.setContentText(message).build();
notificationManager.notify(0 , notification);
}
You need to pass current Context for creating mBuilder object.
Also, please run your app in "Dalvik Virtual machine(DVM)" instead of "Android Run time(ART)".
Check this link : https://source.android.com/devices/tech/dalvik/art.html
Note : Dalvik must remain the default runtime or you risk breaking your Android implementations and third-party applications.
Hope this helps.

Android : Unable to display multiple lines of text in Notification

I am trying to display multiple lines of Text using BigTextStyle in Notification but unable to do so. I am using the code below.
public void sendNotification(View view) {
String msgText = "Jeally Bean Notification example!! "
+ "where you will see three different kind of notification. "
+ "you can even put the very long string here.";
NotificationManager notificationManager = getNotificationManager();
PendingIntent pi = getPendingIntent();
android.app.Notification.Builder builder = new Notification.Builder(
this);
builder.setContentTitle("Big text Notofication")
.setContentText("Big text Notification")
.setSmallIcon(R.drawable.ic_launcher).setAutoCancel(true)
.setPriority(Notification.PRIORITY_HIGH)
.addAction(R.drawable.ic_launcher, "show activity", pi);
Notification notification = new Notification.BigTextStyle(builder)
.bigText(msgText).build();
notificationManager.notify(0, notification);
}
public NotificationManager getNotificationManager() {
return (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
public PendingIntent getPendingIntent() {
return PendingIntent.getActivity(this, 0, new Intent(this,
MainActivity.class), 0);
}
I can't even see 'msgText' in the notification. Any idea why?
Thanks for helping.
Solved!
Code was fine, its just that there was not enough space for big notification. When I disconnected data cable, it got displayed in desired manner. :-)
Thanks to all who tried to help.
Just set notification style to BigText
NotificationCompat.BigTextStyle bigStyle =
new NotificationCompat.BigTextStyle();
bigStyle.setBigContentTitle(title);
bigStyle.bigText(messageBody);
builder.setStyle(bigStyle);
Try to set content intent value as well.
Notification.Builder builder = new Notification.Builder(this);
builder.setContentTitle("Big text Notification")
.setContentText("Big text Notification")
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setContentIntent(pi) // <- this new line
.setPriority(Notification.PRIORITY_HIGH)
.addAction(R.drawable.ic_launcher, "show activity", pi);
.setPriority(Notification.PRIORITY_HIGH) - this line solved my problem. The multiline conserve size.

Categories

Resources