Notifications not shown in Android Froyo - java

I'm trying to run the following, which is ripped from an internet tutorial:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// Prepare intent which is triggered if the
// notification is selected
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);
//Intent intent = new Intent(this, ReceiveAndGoHome.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, homeIntent, 0);
// Build notification
// Actions are just fake
Notification noti = new NotificationCompat.Builder(this)
.setContentTitle("Fraz Go Home!!!")
.setContentTitle("Fraz Go Home!!!")
.setContentText("Fraz Go Home!!!")
.setContentIntent(pIntent)
.addAction(R.drawable.ic_launcher, "Call", pIntent)
.addAction(R.drawable.ic_launcher, "More", pIntent)
.addAction(R.drawable.ic_launcher, "And more", pIntent).build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(25, noti);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
When I run the above in a froyo AVD, I do not see a notification appear at all - am I missing something obvious?

You forgot to set the small icon. Add this:
.setSmallIcon (R.drawable.ic_launcher)
somewhere in your NotificationCompat.Builder chain of methods.
Of course, the launcher drawable is what I used there since it's on hand, you will need to make actual notification icons for a proper UI. This is just to demo the method call you need.
The Android documentation outlines what you need for a Notification to show:
Required notification contents
A Notification object must contain
the following:
• A small icon, set by setSmallIcon()
• A title, set by setContentTitle()
• Detail text, set by setContentText()

Related

How do I get the program to display multiple notifications on the screen?

i cant seem to understand how to display multiple notifications without one overlaying another. In my case it only displays one at the time.
picture 1
My goal is to get it to work like on the screenshot below
picture 2
What should I change or maybe add to my code?
chunk of code assigned for notifications
#Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
Intent intent = new Intent(ctx, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("com.example.romanchuk.appisode.notifyId", id);
intent.putExtra("com.example.romanchuk.appisode.show_id", show_id);
PendingIntent pendingIntent = PendingIntent.getActivity(ctx, sNotificationId /* Request code */, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.addLine(message);
NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
bigText.bigText(message);
bigText.setBigContentTitle(getString(R.string.app_name));
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
Notification notification = null;
notification = builder.setSmallIcon(R.mipmap.ic_launcher).setTicker(title).setWhen(0)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setColor(getResources().getColor(R.color.color_accent))
.setContentTitle("Appisode")
.setContentIntent(pendingIntent)
.setFullScreenIntent(pendingIntent, true)
.setContentText(message)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setStyle(inboxStyle)
.setSmallIcon(R.drawable.small_icon)
.setSound(defaultSoundUri).build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(sNotificationId++, notification);
}
If you want to show multiple notifications, You Notification id should be different, If the notification id already exists in the notifications it will override that notification.
notificationManager.notify(sNotificationId++, notification);
In this sNotificationId should be different for all notification
If your id or show_id is int and not constant and if it will be different for each notification you can use that also as notification id.
Or try to give different tag for each notification like this,
notificationManager.notify(String.valueOf(System.currentTimeMillis()), sNotificationId++, notification);

How to create a chronometer in a notification Android?

I have the following method below:
public NotificationCompat.Builder createNotification(Context context) {
Intent intent = new Intent(this, MapsActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
boolean running = true;
builder = new NotificationCompat.Builder(context)
.setContentText("conteúdo")
.setContentTitle("titulo")
.setSmallIcon(R.drawable.ic_today_black_24dp)
.setAutoCancel(false)
.setOnlyAlertOnce(true)
.setOngoing(running)
.setContentIntent(
PendingIntent.getActivity(context, 10,
new Intent(context, MapsActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP),
0)
)
.addAction(running ? R.drawable.ic_stop_black_24dp
: R.drawable.ic_play_arrow_black_24dp,
running ? "Pause"
: "play",
pIntent)
.addAction(R.drawable.ic_stop_black_24dp, "Stop",
pIntent);
notificationManager.notify(0, builder.build());
return builder;
}
In which launched a notification in the status bar, as shown below in the first notification:
To link the notification I do this:
NotificationCompat.Builder notification = createNotification(this);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, notification.build());
I would like to create a Chronometer in a notification, exactly as it appears in the Strava notification (second notification of the image), as shown above.
How to create a Chronometer in a notification?
So creating the App Widget layout is simple if you know how to work with Layouts. However, you have to be aware that App Widget layouts are based on RemoteViews, which do not support every kind of layout or view widget. Anyway if you need asistance with App Widget layout here is some guaidance:
https://developer.android.com/guide/practices/ui_guidelines/widget_design.html
Luckily for you RemoteViews support Chronometer as you can see from developer webiste: https://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout
A think you can acces your Chronometer as usually and you could do something like this depends do you want it to pause, resume, whatever:
remoteView.setChronometer(R.id.myChronometere, SystemClock.elapsedRealtime(),
null, false); //pausing
You can use built in chronometer:
builder = new NotificationCompat.Builder(context)
builder.setUsesChronometer(true)
...
builder.build()

System notification from SyncAdapter:onPerformSync

I have sync adapter that performs some operation in background. To notify my main activity about sync operation status, I used broadcast receivers, so my activity is able to receive messages from sync adapter. It works fine. However, I also need to display notification on android status bar, that indicates some sync results.
So I wrote simple method reponsible to disaply system notification:
private void sendNotification(Context ctx, String message)
{
Intent intent = new Intent(ctx, this.getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(ctx)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Mobile Shopping")
.setContentText(message)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_SOUND | Notification.FLAG_SHOW_LIGHTS)
.setLights(0xff00ff00, 300, 100)
.setPriority(Notification.PRIORITY_DEFAULT);
//.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 , notificationBuilder.build());
}
Then, above method is called in onPerform sync:
#Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult)
{
.................
sendNotification(context, message);
}
Context is retrieved from constructor. It works without any problems, notification is showing.
However I also need to show main activity after user cicks on notification. So I believe I need to create PendingIntent and pass it to my notification builder (as it's commented in my code). But to pass main activity object to my sync adapter? Notification can be also displayed after auto sync finished.
Any tips?
So, I figured it out. Solution is to create pending intent this way:
Intent notificationIntent = new Intent(ctx, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, 0);
So after user clicks to notification, main activity will be shown.

Notification with pending intent won't make phone call

I have a notification with a pending intent and once clicked it is supposed to dial a number. The notification comes up but once clicked nothing happens. I have set permission for the app to make calls in the android manifest file as follows:
<uses-permission android:name="android.permission.CALL_PHONE" />
Below is the code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rating);
addListenerOnRatingBar();
NotificationCompat.Builder myNotification = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText("Calling 021-12345678")
.setContentTitle("Phone Call Notification");
Intent phoneCall = new Intent(Intent.ACTION_CALL);
phoneCall.setData(Uri.parse("tel:021-12345678"));
PendingIntent phoneCallIntent = PendingIntent.getActivity(this, 0, phoneCall, PendingIntent.FLAG_UPDATE_CURRENT);
myNotification.setContentIntent(phoneCallIntent);
NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, myNotification.build());
}
Any ideas?
Well, this is actually workd (LOL).
But since you are using a pending intent, it needs a triger to launch (Click on the notification and it will call ;-).
Try to replace to a simple intent.
Like this:
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "Your Phone_number"));
startActivity(intent);
And it will work like a charm.
P.S. I'm don't really understand why are using a notification, since when you call someone the call is taking control over the screen.

How to clear notification on notification action click?

I'm developing an android app where I display notifications with actions. But on action click notification not clearing, It stuck in that shade. How do I clear a notification on action click?
MY CODE
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
Intent intent = new Intent(this, SettingsActivity.class);
PendingIntent openSettingsActivity = PendingIntent.getActivity(this,1, intent, PendingIntent.FLAG_CANCEL_CURRENT);
notificationBuilder.addAction(R.drawable.ic_notification_button, "Settings", openSettingsActivity);
notificationBuilder.setPriority(Notification.PRIORITY_MAX);
notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
notificationBuilder.setContentTitle(title);
notificationBuilder.setContentText(text);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setColor(color);
notificationBuilder.setSmallIcon(R.drawable.ic_notification);
notificationBuilder.setContentIntent(openSettingsActivity);
final NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1,notificationBuilder.build());
Hiding notifications should be processed in the place where the intent is sent.
In your current code:
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
Intent intent = new Intent(this, SettingsActivity.class);
intent.putExtra("hide_notification", true); //add boolean to check later in activity if it should remove notification on activity create
And in your activity smth like this, to check if it should remove notification:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//check for notification remove
boolean hideNotification = getIntent().getBooleanExtra("hide_notification", false);
if (hideNotification) {
NotificationManagerCompat nmc = NotificationManagerCompat.from(this);
nmc.cancel(1); //1 - is your notification id
}
}
Depends on what you want, maybe it will be better to call that not in onCreate() but onStart()

Categories

Resources