How clean notification when close app like spotify? - android

I have a service for radio, that show a notification to identify stop and playing, but when I kill the application by swipe, I can't clear the notification
PendingIntent openAppIntent = PendingIntent.getActivity(RadioService.this, 0, new Intent(RadioService.this, GeneralActivity.class), 0);
Notification notification = new NotificationCompat.Builder(RadioService.this)
.setSmallIcon(R.drawable.ic_pause_lx)
.setContentTitle(getResources().getString(R.string.radio) + " " + getResources().getString(R.string.app_name))
.setAutoCancel(true)
.setContentIntent(openAppIntent)
.addAction(android.R.drawable.ic_media_pause, getString(R.string.stop), PendingIntent.getBroadcast(RadioService.this, 0, new Intent(ACTION_PAUSE), 0))
.addAction(0, getString(R.string.open), openAppIntent)
.build();
mNotificationManager.notify(NOTIFICATION_ID, notification);
registerReceiver(mActionsReceiver, new IntentFilter(ACTION_PAUSE));

Call this in you activity's onDestroy():
NotificationManager ntfManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
ntfManager.cancelAll();

Related

Notification manager has stopped working

This issue has been solved. See my solution below.
I just completed converting my messaging app to FCM. You can see the process I have been through here. Now that it is done, my notifications no longer work. If my FirebaseMessagingService gets a message and the main app is not active, I create a notification on the phone I'm running on.
This has been running for years correctly on GCM. When I trace through the code, it all executes ok - just no notification shows up in the tray. I can't imagine what Firebase would have to do with this.
This is the code that gets called from the FirebaseMessagingService. This code has been running for years just fine . . .
public static void raiseNotification( String username, String mesText, int count)
{
String message = "From: " + username + " " + mesText;
if (count > 1)
{
count--;
message = message + " + " + count + " more";
}
NotificationCompat.Builder b = new NotificationCompat.Builder(GlobalStuff.GCT);
Intent intent = new Intent(GlobalStuff.GCT, MainActivity.class);
intent.putExtra("whattodo", username);
intent.setAction(Long.toString(System.currentTimeMillis())); //just to make it unique from the next one
PendingIntent pIntent = PendingIntent.getActivity(GlobalStuff.GCT, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
b.setContentTitle("New SafeTalk Message")
.setSmallIcon(R.drawable.ticon)
.setContentText(message)
.setTicker("New SafeTalk Message")
.setContentIntent(pIntent)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setAutoCancel(true);
//.addAction(R.drawable.smallredball, "Read Now", pIntent)
//.addAction(R.drawable.smallquestion, "hello there", pIntent);
NotificationManager mgr = (NotificationManager)GlobalStuff.GCT.getSystemService(NOTIFICATION_SERVICE);
mgr.notify(0, b.build());
}
This issue is solved. Once you write an app for Android, Google will have you working full time for the rest of your life just to keep it working. They are breaking change demons. Turns out this notification problem has nothing to do with Firebase (which is itself the mother of all breaking changes).
Google changed the requirements on how to send a notification in Oreo. Google designed this change so that if your app is running on Oreo and you haven't made the change your notification simply won't work - hope nobody was building notifications that were important. In Oreo they require a channelId.
Here is code that works in Oreo . . .
Actually this code does not completely work in Oreo. See my next post regarding notifications in Oreo
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.default_notification_channel_id);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setContentTitle("FCM Message")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

Android: Push Notification has no Sound/Vibration/Light

I'm not getting sound, vibration or light when receiving a notification. Can someone please tell me what I'm missing?
Also I have some other questions:
1.) I only get the specified icon when the app is open. When the app is closed I get the android logo icon (I guess thats because I havent defined an app icon yet).
2.) The ticker text is only shown when the app is open.
3.) When the app is open I dont get the content text, only the content title.
SOLUTION: This happens because I am using com.google.android.gms:play-services-gcm:8.4.0 over the previous version.
Make sure that on the server you send a notification array containing only the key/value pair e=0 while sending your message information in the data array.
This problem has already a great answer here: After updating Google play services to 8.4.0 push notifications displayed by themselves
This is my Source:
public class MyGcmListenerService extends GcmListenerService {
private final static String TAG = "GCM_Listener";
#Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
Log.d(TAG, "From: " + from + " (" + message);
// Sets an ID for the notification
SharedPreferences sharedPreferences = getSharedPreferences(getString(R.string.sharedPreferenceStore_default), Context.MODE_PRIVATE);
int mNotificationId = sharedPreferences.getInt("id_notification", 0);
mNotificationId++;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.all_picks_made_indicator)
.setAutoCancel(true)
.setContentTitle("Product - " + mNotificationId)
.setContentText(message)
.setTicker("Product Notification received");
// Because clicking the notification opens a new ("special") activity, there's no need to create an artificial back stack.
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, DetectLoginActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
Notification notification = mBuilder.build();
notification.defaults = Notification.DEFAULT_ALL;
mNotifyMgr.notify(mNotificationId, notification);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("id_notification", mNotificationId);
editor.commit();
}
}
Custom Sound for Push Notification
notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.pop);
In your code change
Notification notification = mBuilder.build();
notification.defaults = Notification.DEFAULT_ALL;
mNotifyMgr.notify(mNotificationId, notification);
To.
notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" +R.raw.pop);
notification.defaults |= Notification.DEFAULT_VIBRATE;
mBuilder.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
mBuilder.setLights(getResources().getColor(R.color.mainColor), 1000, 1000);
you can set color and vibrate on the builder object
You're missing something like this:
long[] pattern = new long[]{0, 100, 200, 100}; //two short beeps
mBuilder.setVibrate(pattern);
Notification note = mBuilder.build();
note.vibrate = pattern;
That will give you vibrations. Look into lights, I don't have that code at hand atm.

Create custom notification android

How to create custom default notification? I am using Remote view to create the notification. but its not what was expect. Below is the image attched. I want to create the notification as that, with two button at bottom(like The Big Meeting is defined.
Any tutorial will be really helpful.
Below is code snippet what I have written.
Intent snoozeIntent = new Intent(LockableActivity.INTENT_SNOOZE);
PendingIntent pendingSnoozeIntent = PendingIntent.getBroadcast(context, 0, snoozeIntent, 0);
Intent gotItIntent = new Intent(LockableActivity.INTENT_GOT_IT);
PendingIntent pendingGotItIntent = PendingIntent.getBroadcast(context, 0, gotItIntent, 0);
RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.view_notification);
remoteView.setTextViewText(R.id.tv_title, entity.getClientName() + " appointment is late");
remoteView.setTextColor(R.id.tv_title, context.getResources().getColor(android.R.color.black));
remoteView.setOnClickPendingIntent(R.id.btn_snooze, pendingSnoozeIntent);
remoteView.setOnClickPendingIntent(R.id.btn_got_it, pendingGotItIntent);
NotificationManager mNotificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(entity.getClientName() + " appointment is late")
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle().bigText(context.getString(R.string.notification_content)))
.setContent(remoteView);
But here I have my xml. Is there other way by which I could DD THE BUTTONs at bottom.
You need to set your remote view to the bigContentView field in the notification
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(entity.getClientName() + " appointment is late")
.setAutoCancel(true).build();
notification.bigContentView = remoteView;
mNotificationManager .notify(0,notification);

Addline of NotificationCompat.InboxStyle is not working

I am trying to make a chat application where my user will get notifications. The volume of notifications are so high, if I will make one entry for each notifications then it will fill up all the places, so I thought of applying BigTextView notifications or Stack of notifications.
I wrote below piece of code:
NotificationManager notificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
if(listMessage.size() <= 5)
listMessage.add(messagetype + ":" + msg);
else
{
listMessage.remove(4);
listMessage.add(messagetype + ":" + msg);
}
Intent notificationIntent = new Intent(this, GcmActivity.class);
notificationIntent.putExtra("title", messagetype);
notificationIntent.putExtra("message", msg);
PendingIntent intent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
NotificationCompat.Builder mBuilder;
mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My MESSENGER")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("MESSAGES"))
.setContentText(msg)
.setAutoCancel(true)
.setLights(Color.WHITE, 1000, 5000)
.setDefaults(Notification.DEFAULT_VIBRATE |
Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS)
.setContentIntent(intent);
NotificationCompat.InboxStyle inboxStyle =
new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle("MESSAGES");
for(int j= 0;j < listMessage.size();j++)
{
inboxStyle.addLine(listMessage.get(j));
}
mBuilder.setStyle(inboxStyle);
notificationManager.notify(0, mBuilder.build());
This seems not to add lines in the notification. it just shows the setContentText and it shows nothing.
You may not have the expanded view of the notification.
You need to swipe down within the notification (it works better with two fingers).

How can i update in background my permanent notification?

My notification shows the battery level. When i close the app the percentage in the notification doesn't update its state. I don't know how i can do so now i post the notification code and i hope someone help me
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
MainActivity.this);
notificationBuilder.setOngoing(true);
notificationBuilder.setContentTitle("Battery Stats Informations");
notificationBuilder.setContentText("Battery level: " + level + "%"
+ " " + "Temp.: " + temperature + "°C");
notificationBuilder.setTicker("Informazioni batteria");
notificationBuilder.setWhen(System.currentTimeMillis());
notificationBuilder.setSmallIcon(R.drawable.icon_small_not);
Intent notificationIntent = new Intent(MainActivity.this,
MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(
MainActivity.this, 0, notificationIntent, 0);
notificationBuilder.setContentIntent(contentIntent);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
Notification notification = new Notification();
notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
mNotificationManager.notify(1, notificationBuilder.build());
mNotificationManager.notify(SIMPLE_NOTIFICATION_ID,
notificationBuilder.build());
I tried everything but nothing goes. How can i do? Thanks
You should create a service if you want to update the notification when your app is not working.
You can check out android developer website

Categories

Resources