I'm new to Android and at the moment I'm developing a MediaPlayer for online streaming. The mediaplayer runs fine and everything, but I'm having a problem with the Notification. When I press "play" the Notification appears in the notification bar and when I click on the notification in the notification bar it start a new activity instead of displaying the current one.
I don't know if I'm using the wrong FLAG for the PendingIntent for this Application.
My Code Looks like This:
public void Notification_Inicialization()
{
Intent intent = new Intent(this,radio_app_3.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
res = getApplicationContext().getResources();
app_notification = new Notification.Builder(this).setTicker(tickerTitle).setContentTitle(title)
.setSmallIcon(R.drawable.android_icon).setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
.setContentIntent(pendingIntent).addAction(R.drawable.android_icon,"Play",pendingIntent)
.getNotification();
notification_layout = new RemoteViews(getPackageName(),R.layout.activity_radio_app_3);
notification_layout.setTextViewText(R.id.button_play,getString(R.string.app_name));
notification_layout.setOnClickPendingIntent(R.id.button_play,pendingIntent);
app_notification.flags = Notification.FLAG_ONGOING_EVENT;
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(uniqueID,app_notification);
}
Hope someone can give me a help with this problem
Thanks in advance
Related
On my radio streaming app i want to stop the player from playing.
How to add a button in the notification to stop or kill the player?
I have this so far:
private void initNotification() {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent intent = PendingIntent.getActivity(StreamService.this, 0, new Intent(this, Radio.class), 0);
builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_ricky)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon))
.setContentTitle("Radio")
builder.setContentIntent(intent);
builder.setOngoing(true);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
Add a close button to the notification layout. Create some intent like this:
final ComponentName srvComp = new ComponentName(getContext(), service.getClass());
Intent close = new Intent(YourRadioService.ACTION_PLAYER_CLOSE);
close.setComponent(srvComp);
mPiClose = PendingIntent.getService(getContext(), 4, close, PendingIntent.FLAG_UPDATE_CURRENT);
Inflate your RemoteView somehow, for example:
RemoteViews smallNotification = new RemoteViews(getContext().getPackageName(), R.layout.notification_small);
smallNotification.setOnClickPendingIntent(R.id.close, mPiClose);
Set the content view on the notification
notification.contentView = smallNotification;
After that, use the notification manager to provide the notification
notificationManager.notify(NOTIFICATION_ID, notification);
Don't forget to handle the Intent. If you need more help look up how some open source music players are doing it.
I have an application which starts a service running on the background. This service periodically issues a constant notification. I would like to be able to press that notification and resume the last activity of the application.
I am creating my notifications as follows:
public static void startNotification(Service service, String message) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(service);
if(prefs.getBoolean("pref_NotificationDisplayed", true)){
// Creates an Ongoing Notification
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(service.getApplicationContext()).setSmallIcon(R.drawable.ic_launcher).setContentTitle("Title").setContentText(message);
Intent toLaunch = new Intent(service.getApplicationContext(),MainActivity.class);
toLaunch.setAction("android.intent.action.MAIN");
toLaunch.addCategory("android.intent.category.LAUNCHER");
PendingIntent intentBack = PendingIntent.getActivity(service.getApplicationContext(), 0,toLaunch, PendingIntent.FLAG_UPDATE_CURRENT);
//PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(intentBack);
NotificationManager mNotificationManager = (NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE);
// Send Notification
Notification primaryNotification = mBuilder.build();
primaryNotification.flags = Notification.FLAG_ONGOING_EVENT;
mNotificationManager.notify(10001,primaryNotification);
}
}
I have tried the solutions from here and here with no luck.
Every time I press the notification a new activity is started up rather than resuming the old activity. Is this because I am issuing the activity from a service ? or am I making an obvious error above ?
Thanks for the help.
are you try
toLaunch.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
inyour intent. check above flag i dont have tried...
My application has a notification in the notifications bar that shows the battery level. The notification works but when the checkbox in the preferences is clicked and the notification appears, i try to pull down the notifications bar and it lags. I don't know way but i think that it's because the notification check every second the battery level but i'm not sure.. I post the code:
#SuppressLint("NewApi")
private void checkPref(Intent intent){
this.registerReceiver(this.batteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
SharedPreferences myPref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
boolean pref_opt1 = myPref.getBoolean("firstDependent", false);
int level= intent.getIntExtra(BatteryManager.EXTRA_LEVEL,-1);
if (pref_opt1){
NotificationManager notifi = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification.Builder(getApplicationContext())
.setContentTitle("Battery Informations")
.setContentText("Battery level"+" "+level+"%")
.setSmallIcon(R.drawable.icon_small_not)
//.setLargeIcon(aBitmap)
.setTicker(level+"%")
.build();
notification.flags = Notification.FLAG_ONGOING_EVENT;
Intent i = new Intent(MainActivity.this, MainActivity.class);
PendingIntent penInt = PendingIntent.getActivity(getApplicationContext(), 0 , i , 0);
notifi.notify(215,notification);
} else {
NotificationManager notifi = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notifi.cancel(215);
}
}
Maybe the problem is this one this.registerReceiver(this.batteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));. But i know only this way to update the notification when the battery level changes.. Some helps please?
Check whether 'if-case' code to show notification is getting executed repeatedly. If so try limiting show notification to once via a flag value and reset the flag during notifi.cancel().
I tried to use the Notification.Builder and the Notification classes.
I tried using this code :
Notification notification = new Notification.Builder(this).build();
notification.icon = R.drawable.ic_launcher;
notification.notify();
but it seems useless.
I only want my app's icon to be added next to the battery icon,wifi icon and 3g icons.. Any way to do that? I appreciate your help.
You have to call the method build() after you have finished describing your notification. Check out the Android reference for an example.
Basically, you have to change your code to the following:
Context context = getApplicationContext();
NotificationCompat.Builder builder = new NotificationCompat.Builder(context )
.setSmallIcon(R.drawable.ic_launcher);
Intent intent = new Intent( context, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, mID , intent, 0);
builder.setContentIntent(pIntent);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notif = builder.build();
mNotificationManager.notify(mID, notif);
Note: this code will only allow to show your icon in the notification bar. If you want it to persist there, you will have to use FLAG_ONGOING_EVENT
You can add your app icon for status bar notification. Try this one
Notification notification = new Notification.Builder(this).setSmallIcon(R.mipmap.ic_launcher).build();
I want notification which will stick on notification bar until music stop playing it.
currently i have written some code in which i can show notification but when i press clear notification button or swap it than it disappear from notification center.
I want notification like spotify which stays on bar until you stop playing music.
Here is my code for notification
int pendingRequestCode = 0;
// final Resources res = getResources();
notificationManager = (NotificationManager) getSystemService(
NOTIFICATION_SERVICE);
Intent i = new Intent(getApplicationContext(),Mainactivity.class);
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_action_search)
.setAutoCancel(true)
.setTicker("test ckick")
.setContentIntent(PendingIntent.getActivity(getApplicationContext(), NOTIFICATION_DEFAULT, i,0));
// Sets a custom content view for the notification, including an image button.
layout = new RemoteViews(getPackageName(), R.layout.notification);
layout.setTextViewText(R.id.notification_title, getString(R.string.app_name));
Intent clickIntent = new Intent();
clickIntent.setAction(ACTION_DIALOG);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), pendingRequestCode, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
layout.setOnClickPendingIntent(R.id.notification_button,pendingIntent);
builder.setContent(layout);
// Notifications in Android 3.0 now have a standard mechanism for displaying large
// bitmaps such as contact avatars. Here, we load an example image and resize it to the
// appropriate size for large bitmaps in notifications.
layout.setImageViewResource(R.id.notification_button, R.drawable.pause);
notificationManager.notify(NOTIFICATION_DEFAULT, builder.getNotification());
waiting for reply
Use setOngoing(true) to indicate that the event is ongoing. You may also wish to remove setAutoCancel(true), as that clears the Notification when the user taps upon it.
notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL