Notification not showing Android 7.0 but showing in Android 6.0 - java

I have a service which displays a notification, the notification works in Android 6.0 and prior but it doesnt show up in 7.0.
Relevant code:
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
PendingIntent pendingIntentMain = PendingIntent.getActivity(this, 0, intent_main, PendingIntent.FLAG_CANCEL_CURRENT);
Notification notificationPopup = new Notification.Builder(this).setContentTitle("Alarm is ON!").setContentText("Click here")
.setContentIntent(pendingIntentMain).setAutoCancel(true).setSmallIcon(R.drawable.acd).setPriority(Notification.PRIORITY_MAX)
.setDefaults(Notification.DEFAULT_ALL).build();
notificationManager.notify(0, notificationPopup);

Please follow up this steps and let me know it works or not
Remove 6ya from Battery Optimization:
1.Go to setting battery.
2.Click the Menu on battery page and choose battery optimization.
3.Clicked on Not optimized and go to all apps.
4.On All Apps locate 6ya app and click on it.
5.It will show a pop-up with Optimize and Don't optimize.
6.Click on Don't optimize and hit DONE.
7.6ya Should be located on the not optimized folder .
8.All set - restart the phone.

I'm afraid I have not been able to reproduce the problem you're seeing.
In my tests, this code creates and displays the notification successfully on Android 5.1, 6.0, 7.0 and 7.1.1:
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService(new Intent(this, MyService.class));
}
}
MyService.java
public class MyService extends Service {
#Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// create and display a notification
Intent intent_main = new Intent(this, MainActivity.class);
PendingIntent pendingIntentMain = PendingIntent.getActivity(this, 0, intent_main, PendingIntent.FLAG_CANCEL_CURRENT);
Notification notificationPopup = new Notification.Builder(this)
.setContentTitle("Alarm is ON!")
.setContentText("Click here")
.setContentIntent(pendingIntentMain)
.setAutoCancel(true)
.setSmallIcon(android.R.drawable.ic_lock_idle_alarm)
.setPriority(Notification.PRIORITY_MAX)
.setDefaults(Notification.DEFAULT_ALL)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationPopup);
return super.onStartCommand(intent, flags, startId);
}
}

Related

android Lollipop. Notifications not working

Faced with the problem of displaying notifications in android Lollipop. When app is open notifications shows app fine, but when the application is closed they are not displaying. On 4x versions of android everything works properly. Here is a method to display them
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void sendNotification(String body, String title, String badge) {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("from_notify", true);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, Integer.valueOf(badge), intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_name)
.setContentTitle(title)
.setContentText(body)
.setPriority(Notification.PRIORITY_HIGH)
.setShowWhen(true)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
I think this is the problem:
when your app is closed,all app's data will be cleared including the notifications and statics data,so if you want to make notifications alive all the time you have to send it from service(create service class and run it in the background).
try this:
public class YourService extends Service {
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
//send your notifications here
return START_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
note that you can send notifications from other classes not just from service class,but you have to run a service in the background because it helps to keep statics data for your app after it is closed.this worked for me.
I hope this will help you.

Instead of pendingIntent, execute function in Notification.addAction() . Is it possible?

I want a method to be executed when I press a button on my notification. For that purpose I am adding an action with a PendingIntent to my notification:
Intent intent = new Intent(context, AlertActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
Notification notification = new Notification.Builder(MainActivity.this)
.setContentTitle("New Notification")
.setContentText("Click Here")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent)
.addAction(R.mipmap.ic_launcher, "Test2", pendingIntent)
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(0, notification);
That works, however I don't want to start an Activity when the user invokes the action. I just need to do some work.
For that purpose I implemented a Service which should be targeted by the PendingIntent instead:
public class MyServices extends IntentService {
public MyServices() {
super("MyServices");
}
#Override
protected void onHandleIntent(Intent intent) {
clearNotification();
}
public void clearNotification() {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(0);
Intent intent = new Intent(MyServices.this, MainActivity.class);
//Starting new activity just to check
startActivity(intent);
}
}
I create the PendingIntent like this:
final Intent intent = new Intent(context, MyServices.class);
final PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
However when I invoke the action on my notification nothing happens. What am I doing wrong?
A Notification is not part of your application. It is managed by the OS. It just so happens that there are APIs you can use to show/cancel/etc notifications.
A pending intent allows for external code (Notifications for example) to launch your app/activity/service/broadcastreceiver. This cannot be done without a pending intent.
What my task is to execute some piece of code when a specific action button is clicked, and clear notification; without starting any activity
You don't have to start an activity. You can do it in a broadcastreceiver that has no UI. Or, as CommonsWare suggested, use an IntentService, depending on what what you are doing in your "piece of code". IntentServices handle work in a separate thread.

From the notificationbar, to call the working Class

I'm developing an online radio application. The application works in the background. When I click the NotificationManager, radioclass starts working again. I want to call the radioclass that are running. How can I do?
player = MediaPlayer.create(this, Uri.parse("http://...../playlist.m3u8"));
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer player) {
}
});
player.start();
final int notificationID = 1234;
String msg = "mesajjj";
Log.d(TAG, "Preparing to update notification...: " + msg);
NotificationManager mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, RadioClass.class);
//Here RadioClass running again. But RadioClass already running. I should call RadioClass that is running.
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.logotam)
.setContentTitle("Test FM")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(pendingIntent);
mNotificationManager.notify(notificationID, mBuilder.build());
If you want to go back to the same activity you are currently running, you can do it like this:
Add the following tag on your AndroidManifest.xml:
<activity
........
android:launchMode="singleTop" >
........
</activity>
Modify your PendingIntent like this:
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
With this modifications, you ensure that your activity will only have one instance at any given time. On your activity class, you can also override the onNewIntent method:
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
}
This method will handle all of the additional calls to your activity.

Android - Vibrate without displaying new Activity?

I have the following code in an android app. What it currently does is, at the specified time passed with the Calendar when variable, it opens up the RunningActivity (which is blank), vibrates, and sends a notification. Even if I've pressed the home button and it's running in the background, it starts a new blank RunningActivity and vibrates and sends a notification. I'm trying to figure out how to do all the stuff in the RunningActivity (Vibrate and send a notification) without opening up the blank RunningActivity, allowing the application to stay in the background.
I do NOT need help with actually calling the notification or vibration. I just need to know how to run the actions in the RunningActivity onCreate at a specific time witout opening/showing the RunningActivity. As seen below, I setup a PendingIntent with an AlarmManagager, the issue is that it is launching an Activity and showing it when all I want it to do is vibrate/send a notification.
public void startAlarm(Activity activity, Calendar when){
currentTimerHour = when.get(Calendar.HOUR);
currentTimerMin = when.get(Calendar.MINUTE);
Intent intent = new Intent(activity, RunningActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(activity, 12345, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager)activity.getSystemService(Activity.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), pendingIntent);
}
RunningActivity Class:
public class RunningActivity extends Activity {
#Override
public void onCreate(Bundle bundle){
super.onCreate(bundle);
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(500);
NotificationCompat.Builder mBuilder;
mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!");
Intent resultIntent = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
}
}
To send Notification From Background:
public void createNotification(Context context) {
// Prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(context,Myexample.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
Notification noti = new Notification.Builder(context)
.setContentTitle("My Title")
.setContentText("My message.")
.setSmallIcon(R.drawable.app_icon)
.setContentIntent(pIntent).build();
#SuppressWarnings("static-access")
NotificationManager notificationManager =
(NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
// Hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
noti.flags |= Notification.FLAG_SHOW_LIGHTS;
notificationManager.notify(0, noti);
}
You can use service. Add your vibrator class on Service and call it when you need need it. You can also create a method on your class and call it when you need it.
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(500);

Android deleteIntent not working? What's wrong with my code?

I'm trying to detect when one of my notifications is cleared (either by swiping it away individually, or through the "delete all" notifications button). I'm trying to dismiss an AlarmManager alarm, but so far, it hasn't been working for me. What's wrong with my code?
onCreate(Bundle savedInstanceState) {
...
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notif = new Notification(R.drawable.flag_red_large, reminderName, System.currentTimeMillis());
notif.deleteIntent = PendingIntent.getService(this, notifID, new Intent(this, CleanUpIntent.class), 0);
//Destroy the activity/notification.
finish();
}
class CleanUpIntent extends IntentService {
public CleanUpIntent() {
super("CleanUpIntent");
}
#Override
protected void onHandleIntent(Intent arg0) {
System.out.println(">>>>>>>>>>>>>>>>>>>" + "Repeating Alarm Cancelled...");
Intent i = new Intent("com.utilityapps.YouForgotWhat.DisplayReminderNotification");
int reminderID = i.getExtras().getInt("reminderID");
PendingIntent displayIntent = PendingIntent.getBroadcast(this, reminderID, i, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(displayIntent);
displayIntent.cancel();
}
}
}
As you can see, I threw in a System.out.println() into my sub-class to check to see if my code is even reaching that class. I can't see that line in my LogCat output, so I'm assuming that for some reason, my PendingIntent.getService() line isn't working. How do I fix this issue? Thanks! :D

Categories