0
I followed the example given here of stackoverflow,
Notifications in a given time and on mobile phone
but only the service is started, the notification is not launched. Follow my code here, how I resolve this situation?
public class StartUpBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
context.startService(new Intent(context,
ShowNotificationService.class));
}
}
}
public class ShowNotificationService extends Service {
@Override
public void onCreate() {
super.onCreate();
String date_s = "2017-06-26 15:11:00";
// *** note that it's "yyyy-MM-dd hh:mm:ss" not "yyyy-mm-dd hh:mm:ss"
SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = null;
try {
date = dt.parse(date_s);
} catch (ParseException e) {
e.printStackTrace();
}
AgendarNotificacao(date);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void AgendarNotificacao(Date data) {
// Obtém um novo calendário e define a data para a data da notificação
Calendar calendar = Calendar.getInstance();
calendar.setTime(data);
// Obtém um alarm manager
AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(getBaseContext().ALARM_SERVICE);
// O id a ser usado no pending intent
int id = (int) System.currentTimeMillis();
// Prepare the intent which should be launched at the date
Intent intent = new Intent(this, CriarNotificacao.class);
// Obtém o pending intent
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Regista o alerta no sistema.
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
}
public class CriarNotificacao extends BroadcastReceiver {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onReceive(Context context, Intent intent) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setContentTitle("lala")
.setContentText("conteudo")
.setAutoCancel(true);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_CANCEL_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(1500);
mNotificationManager.notify(id, mBuilder.build());
}
}
No manifest indico o service <service android:name=".Notification.Shownotificationservice" />
– Wouva
Error appears?
– viana
no, the notification simply does not appear
– Wouva
Registered Broadcastreceiver Create notification on Androidmanifest?
– ramaral
I didn’t register as soon as I can. Thank you!!
– Wouva
Ramaral’s right, it still doesn’t work. Gives this error Process: com.example, PID: 2338 java.lang.Runtimeexception: Unable to start receiver com.example. Notification.Creaturenotification: java.lang.Illegalstateexception: No intents Added to Taskstackbuilder; cannot getPendingIntent Caused by: java.lang.Illegalstateexception: No intents Added to Taskstackbuilder; cannot getPendingIntent
– Wouva
Creating notification is in its own java file (Create notification.java)?
– ramaral
Yes ramaral, this one
– Wouva
I cannot understand what the problem is. If you are doing everything as described in reply to the question Notifications in a given time and on mobile phone should work since that code was tested before responding. So I can be notified when posting a comment use @ramaral instead of only ramaral.
– ramaral
@ramaral I put a log in the create classNotification, even before creating the notification, can you tell me why this log only appears if I turn off the phone? If I have the application open, nothing happens, but if I turn off the phone the log appears and then.
– Wouva
If the notification appears only after connecting the phone is because the notification date/time has passed. Also note that notification is only scheduled by Alarm when you turn on your phone.
– ramaral
@ramaral I’m sorry to bother you, but what if I want the notification to appear even when your phone is blocked, or the app is open?
– Wouva
I still don’t quite understand what you intend to do, however see this reply. Adapt it to your case.
– ramaral
@ramaral qestão, the notification is only released if the mobile phone is switched off? Or is it launched even if the application is running or the phone is blocked? (referring to the example I followed)
– Wouva
Notification is only scheduled by Alarm when you turn on your phone, so you need to turn it off and on. If you want to schedule the notification without disconnecting/calling the Shownotificationservice with
startService()
in an Activity. If scheduled, the notification will be launched whether or not the application is running. The way the implementation of the service was made I do not guarantee that the phone is "awake" if it is blocked. Here’s how to use Powermanager in the Bootservice service response I’ve indicated.– ramaral