Why is Android ignoring the creation of the Notification? - android

I have code, and it just does not work, so I ask anyone to help me out. There is very little info on this specific matter.
MainActivity
public class MainActivity extends Activity {
public final int PENDING_INTENT_ID = 8;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button clickity = (Button)findViewById(R.id.alarm_button);
clickity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Calendar now = Calendar.getInstance();
now.add(Calendar.SECOND, 20);
//Create a new PendingIntent used by the Alarm when it activates
Intent intent = new Intent(v.getContext(), AlarmReciever.class);
intent.setAction("SOME_AWESOME_TRIGGER_WORD");
intent.putExtra("info", "This String shows that the info is actually sent correctly");
PendingIntent pendingIntent = PendingIntent.getBroadcast(v.getContext(), PENDING_INTENT_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT|Intent.FILL_IN_DATA);
//Then Create the alarm manager to send this pending intent and set the date to go off
AlarmManager am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, now.getTimeInMillis(), pendingIntent);
}
});
}
AlarmReciever (Aware I spelled it wrong but since thats how it is, im going with it)
public class AlarmReciever extends BroadcastReceiver{
#Override
public void onReceive(Context c, Intent arg1) {
//get a reference to NotificationManager
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) c.getSystemService(ns);
//Instantiate the notification
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();
Notification.Builder builder = new Notification.Builder(c)
.setTicker(tickerText)
.setWhen(when)
.setContentTitle(arg1.getStringExtra("info"))
.setContentText("Success!!")
.setAutoCancel(true);
Notification notification = builder.getNotification();
mNotificationManager.notify(0, notification);//note the first argument, the ID should be unique
}
}
Manifest
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="com.testproject.AlarmReciever"
android:enabled="true"
android:exported="false" >
<intent-filter>
</receiver>
</application>
That should be everything. I am trying to run it as is and its not showing me anything. I am running it on an emulator is that actually matters.
EDIT: When I say it doesn't work, I mean nothing pops up. It runs fine, but the Notification never pops up.
The issue:
So the issue is narrowed down to Android just ignoring my Notification. Problem is it doesn't tell me why -_- so I can't fix it. Any experts on the matter see something wrong with my code to call a notification? Does it matter that its on an emulator?

I ran into the same issue. If you don't specify an icon when creating a "new Notification()", the notification will not appear.

Well, lesson learned on Notifications. The reason I was getting the error was because an img MUST be added, if not, it will not show! Everything else I had was basically right with the exception of what Vladimir was graciously able to point out. Posting this here incase others are getting a similar issue just testing out the notifications.

Intent intent = new Intent(v.getContext(), AlarmReciever.class);
// intent.setAction("SOME_AWESOME_TRIGGER_WORD"); replace to:
intent.setAction("com.testproject.SOME_AWESOME_TRIGGER_WORD");
It's at least for first look
UPD:
long firstTime = SystemClock.elapsedRealtime();
AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, /* INTERVAL IN MS */ 20 * 1000, /* PendingIntent */ intent);

This is because, you are not setting the notification icon. If you set notificaiton icon it should work. Log itself says that you can't send a notification without an image.

Seems like you forgot to set Icon.. You need to atleast set a default icon..
Try this..
Notification.Builder builder = new Notification.Builder(c)
.setTicker(tickerText)
.setWhen(when)
.setContentTitle(arg1.getStringExtra("info"))
.setContentText("Success!!")
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher); //<--- this one

Related

Notification started from service doesn't open the app on click

Good Morning,
I've made an application that implements GpS Location. I have a service that save my location on LocationChanged event. To avoid that Android close my app I start a notification and all works well. But now, I want that when I click on notification from Action Bar the app come back in foreground.I use fragment and the map fragment is called MappaFragment. I read a lot of messages but It seems not resolve my issue. Below my code, any suggestion is appreciated !
Alex
This is my monitoring Service:
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.getAction().equals(Constants.STARTFOREGROUND_ACTION)) {
Log.i(TAG, "Received Start Foreground Intent ");
Intent notificationIntent = new Intent(getApplicationContext(), LocationMonitoringService.class);
Bundle extras = intent.getExtras();
numeroTraccia = extras.getInt("numeroTraccia");
itinerario = extras.getString("itinerarioRiferimento");
notificationIntent.setAction(Constants.MAIN_ACTION);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
Intent.FLAG_ACTIVITY_CLEAR_TASK);
setGiornoRiferimento(gg.checkDayLong(this));
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),
0, new Intent(getBaseContext(),MappaFragment.class), 0);
Bitmap icon = BitmapFactory.decodeResource(getResources(), com.example.alex.myApp.R.drawable.smallicon );
Notification.BigTextStyle bigText = new Notification.BigTextStyle();
Notification notification = new Notification.Builder(getApplicationContext())
.setContentTitle("MyApp")
.setTicker(getString(R.string.track))
.setStyle(bigText.bigText(getString(R.string.track)))
.setSmallIcon(R.drawable.smallicon)
.setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false))
.setContentIntent(pendingIntent)
.setOngoing(true)
.build();
startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE,
notification);
}
This is my Manifest:
<activity android:name="com.example.alex.myApp.MainActivity"
android:screenOrientation="portrait"
android:configChanges="orientation|screenSize|keyboardHidden" />
<activity
android:name="com.example.alex.myApp.SplashScreen"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="com.example.alex.myApp.services.MonitoringService"
android:enabled="true"/>
<receiver
android:name="com.example.alex.myApp.RestarterBroadcastReceiver"
android:enabled="true"
android:exported="true"
android:label="RestartWhenStopped">
<intent-filter>
<action android:name="com.example.alex.myApp.ActivityRecognition.RestartSensor"/>
</intent-filter>
</receiver>
Thanks in advance !
Alex
First of all,
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getBaseContext(),MappaFragment.class), 0);
this is incorrect. Fragment can't be opened using intent. You need to launch an activity, then redirect to required fragment.
Next thing is, if app is in background and you click on the notification, it will open launcher activity. So in launcher activity you need to check for bundle object and there you can redirect to particular activity or fragment.
Check this post: Push notification works incorrectly when app is on background or not running and Open specific Activity when notification clicked in FCM it will help you to better understand how FCM works when app is in background and foreground.
Create a BroadCast Receiver like below:
public static class ActionReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals("open_activity")) {
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setClass(AppContext.getContext(), MappaFragment.class);
intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(intent);
}
}
}
And then, Use this notificationIntent.setAction("open_activity");
And Change your PendingIntent to:
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),
0, new Intent(AppContext.getContext(), ActionReceiver.class), 0);
NOTE: For USing This you need to Remove These Lines:
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE, notification)
What I'm suggesting is to use a BroadcastReceiver to receive whatever you want to do in Your Notification.
Also, If this isn't working try Adding .setAction(Intent.ACTION_MAIN) to Your Notification like Below:
Notification notification = new Notification.Builder(getApplicationContext())
.setContentTitle("MyApp")
.setTicker(getString(R.string.track))
.setStyle(bigText.bigText(getString(R.string.track)))
.setSmallIcon(R.drawable.smallicon)
.setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false))
.setContentIntent(pendingIntent)
.setOngoing(true)
.setAction("open_activity")
.build();

Notification created in BroadcastReceiver doesn't work, on app close

This is my code. It works fine, if I use this in a separate Android studio project. But when I integrate the code to another project it is causing a problem.
If app is open I am able to receive the notifications. But on app close, the notifications are not received.
Also I have observed one thing - I set my alarm at 1:10 (let's say) and close, and re-open the app at 1.09. I am receiving the notification at 1.10 !!.
I am not able to identify what's happening. Please tell me even if am doing silly mistake, Thank you.
public class BroadCast {
public void broadcastIntent(int selected, Context context, long userMilli) {
Intent intent = new Intent(context.getApplicationContext(), ReminderReceiver.class);
Bundle bundle = new Bundle();
bundle.putInt("selected", selected);
intent.putExtras(bundle);
intent.setAction("android.media.action.DISPLAY_NOTIFICATION");
intent.addCategory("android.intent.category.DEFAULT");
Calendar calendar = Calendar.getInstance();
long curMilli = calendar.getTimeInMillis();
PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), selected, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT)
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + (userMilli-curMilli), pendingIntent);
else
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + (userMilli-curMilli), pendingIntent);
}
}
Above code creates an intent and broadcast using alarmManager.
public class ReminderReceiver extends BroadcastReceiver {
public ReminderReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
int selected = bundle.getInt("selected");
sendNotification(context, selected);
}
private void sendNotification(Context context, int selected) {
String notificationContentText = "String to display";
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setSmallIcon(R.drawable.alarm)
.setContentTitle("Title")
.setPriority(Notification.PRIORITY_MAX)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentText(notificationContentText)
.setColor(Color.rgb(58,95,205));
notificationManager.notify(selected, builder.build());
}
}
Above code building the notification
This is my AndroidManifest file
<receiver
android:name=".ReminderReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.media.action.DISPLAY_NOTIFICATION"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
It was not the problem of BroadcastReceiver or the Service. I had to change my notification settings in my mobile. I use Asus, there you have to check power management option. If it is in power saving mode then the notifications are denied on app close.

Android - Create Repeating Notification At Set Time

I have looked through a ton of documentation and stackoverflow questions on how to set a repeating notification set to a certain time but can not get any of them to work. Here is what I have right now.
The method I set up the AlarmManager in:
//set alarm method
private void setAlarm() {
if(enableCheckBox.isChecked()) {
//save time / title / message
mTinyDB.putInt(Constants.SAVED_HOUR, timePicker.getCurrentHour());
mTinyDB.putInt(Constants.SAVED_MINUTE, timePicker.getCurrentMinute());
mTinyDB.putString(Constants.SAVED_TITLE, titleEditText.getText().toString().trim());
mTinyDB.putString(Constants.SAVED_MESSAGE, messageEditText.getText().toString().trim());
mTinyDB.putBoolean(Constants.SAVED_NOTIFICATION_ENABLED, enableCheckBox.isChecked());
//create repeating notification
Intent intent = new Intent(NotificationActivity.this, Notify.class);
AlarmManager manager = (AlarmManager) getSystemService(Activity.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(this,
0, intent, 0);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, mTinyDB.getInt(Constants.SAVED_HOUR, 0));
cal.set(Calendar.MINUTE, mTinyDB.getInt(Constants.SAVED_MINUTE, 0));
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
manager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent);
}
}
The Notify class which extends BoradcastReceiver:
public class Notify extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
TinyDB mTinyDB = new TinyDB(context);
Notification builder = new Notification.Builder(context)
.setContentTitle(mTinyDB.getString(Constants.SAVED_TITLE))
.setContentText(mTinyDB.getString(Constants.SAVED_MESSAGE))
.setSmallIcon(R.drawable.ic_action_check)
.setContentIntent(pIntent)
.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, builder);
}
}
Am I doing this completely wrong? I can't get anything to come up (at least at the set time). Help or guidance is appreciated :)
You're using PendingIntent.getService, but your intent is not for a service. For a BroadcastReceiver, you should be using PendingIntent.getBroadcast.
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
The problem you're running into is that you can't set an exact, repeatable alarm in Android. If you want your repeating alarm to occur at an exact time, you must set a one time exact alarm and recreate it after the alarm goes off in your code.
Citation: Documentation for setRepeating
In order for the system to be able to launch a component, it should be registered in your manifest:
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mypackage.myapplication" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".Notify" />
</application>
</manifest>

alarm reminder not working

I am trying to create reminder using alarm manager. But seems it's not working. I am trying to set multiple reminder using different id but my broadcastreceiver not getting called. I am not seeing any notification or nor any sound.
I have tried 30-40 times.
I am setting date to calender like 25/11/2014
Here is myactivity code which setting alarm notification.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 2014);
calendar.set(Calendar.MONTH, 11);
calendar.set(Calendar.DAY_OF_MONTH, 25);
calendar.set(Calendar.HOUR_OF_DAY, 19);
calendar.set(Calendar.MINUTE, 30);
//calendar.set(Calendar.SECOND, 1);
Intent myIntent = new Intent(CreateReminder.this, MyReceiver.class);
myIntent.putExtra("reminder_id",reminderid);
myIntent
.setAction("com.sandeep.alarm.REMINDER");
PendingIntent pendingIntent = PendingIntent.getBroadcast(
CreateReminder.this, reminderid, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
pendingIntent);
MyReceiver.class
public class MyReceiver extends BroadcastReceiver {
private Ringtone r;
#SuppressWarnings("deprecation")
#Override
public void onReceive(Context context, Intent intent) {
int ID=intent.getExtras().getInt("reminder_id");
Log.i("CreateReminder", "reminder_id:-"+ID);
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, "reminder", when);
String title = context.getString(R.string.app_name);
String subTitle = "Please complete task";
Intent notificationIntent = new Intent(context, ReminderActivity.class);
notificationIntent.putExtra("reminder_id", ID);
PendingIntent intent1 = PendingIntent.getActivity(context, 0,notificationIntent, 0);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
notification.setLatestEventInfo(context, title, subTitle, intent1);
//To play the default sound with your notification:
//notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_INSISTENT;
//notification.defaults |= Notification.;
notificationManager.notify(0, notification);
Uri notification1 = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
r = RingtoneManager.getRingtone(context, notification1);
r.play();
new Handler().postDelayed(new Runnable(){
public void run() {
r.stop();
}
}, 10000);
}
}
I am registering my receiver in AndroidManifest.xml with permissions.
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.sandip.remindme.CreateReminder"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.sandip.remindme.ReminderActivity"></activity>
<receiver android:name="com.sandip.remindme.MyReceiver"
android:exported="false" >
<intent-filter>
<action android:name="com.sandeep.alarm.REMINDER" />
</intent-filter>
</receiver>
</application>
I don't understand why it's not working. Please give me some hints or reference.
You are not sending your broadcast after you set your action in your intent.
Intent myIntent = new Intent(CreateReminder.this, MyReceiver.class);
myIntent.putExtra("reminder_id",reminderid);
myIntent
.setAction("com.sandeep.alarm.REMINDER");
sendbroadcast(myIntent);//YOU FORGOT TO ADD THIS
I have compared your code to mine and have a couple of things for you to try...
Is com.sandeep.alarm a real namespace? The activities within your app have the differing namespace com.sandip.remindme so you could use this ("com.sandip.remindme.REMINDER") for your action name.
Your intent is created with a specific context and class target. Try constructing it with just the action name, then you will know if the action name is the issue:
Intent myIntent = new Intent("com.sandip.remindme.REMINDER")
I'm also working on Alarm remainder and I've implemented successfully in my app. you can download the example code in the following link. I hope it helps you. Please note that alarm takes 30secs to 1 min to call broadcast receiver. i.e., if you set alarm at 3:30:00pm then broad cast receiver will be called at around 3:30:30pm.
https://www.dropbox.com/s/t2m5ph8s8f17v6m/AndroidAlarmManager.zip?dl=0
Thanks
Ramesh
You're never sending the broadcast.
sendbroadcast(myIntent);
Add that directly after you do .setAction, and the code should work just fine.

Create timed notification (e.g. for events) in Android

For some Android applications, I would like to integrate the following feature:
The user can define a time when he wants to be reminded of something. When the time has come then, the application should create a notification in the notification bar even when the user doesn't use the app at this moment.
For this purpose, the classes AlarmManager, NotificationManager und Notification.Builder are the ones to look at, right?
So how do I create a timed notification in advance? My code (so far) is this:
Add this under to the AndroidManifest to register the broadcast receiver:
<receiver android:name="AlarmNotificationReceiver"></receiver>
Create a new class file which handles the alarm that it receives:
public class AlarmNotificationReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String additionalData = extras.getString("displayText");
// show the notification now
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification mNotification = new Notification(R.drawable.ic_launcher, context.getString(R.string.app_name), System.currentTimeMillis());
PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0); // open MainActivity if the user selects this notification
mNotification.setLatestEventInfo(context, context.getString(R.string.app_name), additionalData, pi);
mNotification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_SOUND;
mNotificationManager.notify(1, mNotification);
}
}
}
Use this code (for example in MainActivity) to set the alarm to 3 seconds from now:
Intent i = new Intent(this, AlarmNotificationReceiver.class);
i.putExtra("displayText", "sample text");
PendingIntent pi = PendingIntent.getBroadcast(this.getApplicationContext(), 234324246, i, 0);
AlarmManager mAlarm = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
mAlarm.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+3*1000, pi);
What do I need to change to make this work? Thank you!
The two problems are:
The notification's text does not change when I change it in code. It only changes when I change the requestCode in PendingIntent.getBroadcast(...). What is this request code all about? Can it be a random value or 0?
After rebooting my phone, the "planned" notification, or the alarm, is gone. But now I've seen that this is normal behaviour, right? How can I circumvent this?
Not sure about part 1, but for part 2 the general approach is to intercept the BOOT_COMPLETED intent and use that to re-register all alarms. This does unfortunately mean that for each alarm you have registered with the alarm manager you have to store it in your app's db as well.
So, you'll need a broadcast receiver to intercept the BOOT_COMPLETED intent:
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// get your stored alarms from your database
// reregister them with the alarm manager
}
}
To get the BOOT_COMPLETED intent, you must put the following permission in your manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
And the BootReceiver also needs to be registered in your manifest with the following intent filter:
<receiver android:enabled="true" android:name=".receiver.BootReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
It's important to note that if your app is installed to the sdcard, it can never receive the BOOT_COMPLETED intent. Also, it's worth noting that this implementation is a bit naive in that it executes code immediately on booting which can slow the user's phone down at startup. So, I recommend delaying your execution for a few minutes after intercepting the boot intent.
I personally would do it without a Broadcast Receiver. I'd get the AlarmManager to fire the intent to start a seperate Activity, rather than receiver. Then this new Activity could make the notification for you. I'm not sure if this is a better way, but it seems less complicated to me.
Edit: A Service would probably be better still than an Activity
In your MainActivity:
Intent i = new Intent(getBaseContext(), NotificationService.class);
PendingIntent pi = PendingIntent.getService(getBaseContext(), 0, i, 0);
AlarmManager mAlarm = (AlarmManager) Context.getSystemService(Context.ALARM_SERVICE);
mAlarm.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+10*60*1000, pi);
Your Service:
public class NotificationService extends Service {
public int onStartCommand(Intent intent, int flags, int startId) {
//Create the notification here
return START_NOT_STICKY;
}
Your Manifest:
<service android:name="com.android.yourpath.NotificationService"></service>

Categories

Resources