Notification message does not work - android

I followed a tutorial to display notification message in a certain time but I don't know why it does not work
Here is the method
//notification
private void startAlarm() {
AlarmManager alarmManager = (AlarmManager) this.getSystemService(this.ALARM_SERVICE);
Calendar Calendar_Object = Calendar.getInstance();
Calendar_Object.set(Calendar.MONTH, 12);
Calendar_Object.set(Calendar.YEAR, 2014);
Calendar_Object.set(Calendar.DAY_OF_MONTH, 31);
Calendar_Object.set(Calendar.HOUR_OF_DAY, 9);
Calendar_Object.set(Calendar.MINUTE, 06);
Calendar_Object.set(Calendar.SECOND, 0);
Intent intent = new Intent(ManagePassengers.this, AlarmReciver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(ManagePassengers.this, 0, intent, 0);
alarmManager.set(AlarmManager.RTC, Calendar_Object.getTimeInMillis(), pendingIntent);
}
Broadcast Class
public class AlarmReciver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// When our Alaram time is triggered , this method will be excuted (onReceive)
// We're invoking a service in this method which shows Notification to the User
Intent myIntent = new Intent(context, NotificationService.class);
context.startService(myIntent);
}}
Service class
public class NotificationService extends Service {
private NotificationManager mManager;
#Override
public void onCreate() {
super.onCreate();
}
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
// Getting Notification Service
mManager = (NotificationManager) this.getApplicationContext()
.getSystemService(
this.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(), ManageRides.class);
Notification notification = new Notification(R.drawable.icon,
"See My App something for you", System.currentTimeMillis());
...
mManager.notify(0, notification);
}
....
}
manifest edits
<uses-permission android:name="android.permission.WAKE_LOCK" />
<activity android:name=".AlarmReciver" />
<activity android:name=".NotificationService" />
I wait for the time that I specified in the calendar object but nothing appears, I don't know if I missed any thing
any help will be appreciated

Your registration of Service and Receiver components are wrong. Example of Service and Receiver in Manifest is shown below:
<service
android:name="MyService"
android:icon="#drawable/icon"
android:label="#string/service_name"
>
</service>
<receiver android:name="MyScheduleReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Check these tutorials:
http://www.vogella.com/tutorials/AndroidServices/article.html
http://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html

Related

Alarm manager and broadcast receiver in force closed app

I want to do some network job periodically even when app if force closed.
Now it works until it's force closed.
What i am missing?
Also if i add to manifest this: android:process=":remote" - it's not triggering onReceive method (like app is force closed), but in logs i found that:
V/AlarmManager: triggered: cmp=com.cryptorulezz.cryptosignals/.Code.Alarm Pkg: com.cryptorulezz.cryptosignals
My code:
public class Alarm extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();
// Put here YOUR code.
Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example
System.out.println("ALARM !!!!!!!!!!!!!!!!!!!!");
wl.release();
}
public void setAlarm(Context context)
{
AlarmManager am =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
//Intent i = new Intent(context, Alarm.class);
Intent i = new Intent("Coinsider.START_ALARM");
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 1, pi); // Millisec * Second * Minute
}
public void cancelAlarm(Context context)
{
Intent intent = new Intent(context, Alarm.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
}
How i set alarm in MainActivity:
Alarm alarm = new Alarm();
alarm.setAlarm(this);
Manifest:
<receiver android:name=".Code.Alarm" android:exported="false">
<intent-filter>
<action android:name="Coinsider.START_ALARM" >
</action>
</intent-filter>
</receiver>
Once the app gets force killed, it won't receive the intent and the intent filter won't be triggered. To overcome this, I suggest a sort of watchDog, that relies on some system events (like android.intent.action.BOOT_COMPLETED), that simply checks if one of your process is alive, and fires it if not. In the manifest, you 'd have something like this:
<receiver
android:name="it.angelic.receivers.WatchDogSetupReceiver"
android:process=":souliss_process">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.USER_PRESENT"/>
</intent-filter>
</receiver>
The class WatchDogSetupReceiver would then check if the dog is alive, and fire a new one if needed:
Intent i = new Intent(ctx, WatchDogEventReceiver.class); // explicit intent
PendingIntent patTheDog = PendingIntent.getBroadcast(ctx, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, now.getTimeInMillis(), 5000,
patTheDog);
Last, WatchDogEventReceiver would simply do the required un-killable job. It is important that the watchdog stays light, as it will be fired upon every screen on event. This solution is non-optimal, but works even after force-kill:
public class WatchDogEventReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context ctx, final Intent intent) {
Log.d(Constants.TAG + ":WatchDog", "WatchDog.onReceive() called, looking for Dataservice:");
ActivityManager manager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
//Fire Service
Intent eventService = new Intent(ctx, YourDamnService.class);
ctx.startService(eventService);//sempre, ci pensa poi lui
}

Android notifications after "all app close" button

I am using notifications in my application and everything works well - the notification is showing at desired time to me, even after device reboot.
However, when I click the special button to close all apps, so it closes my app, the notification is not showing at all.
"The special button to close all apps" - I mean the one where you can list all the background apps and then with one click kill them all.
I am testing everything on OnePlus 3T.
Is it possible to run the service again after that "kill all apps" click ?
manifest:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
...
<receiver
android:name="com.saga.thewitcherquotes.QuoteOfTheDayReceiver"
android:enabled="true"
android:exported="false" >
</receiver>
<receiver android:name="com.saga.thewitcherquotes.DeviceBootReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name="com.saga.thewitcherquotes.QuoteOfTheDayIntentService"
android:exported="false"></service>
BootReceiver:
public class DeviceBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
// on device boot compelete, reset the alarm
Intent alarmIntent = new Intent(context, QuoteOfTheDayReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar current = Calendar.getInstance();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 21);
calendar.set(Calendar.MINUTE, 30);
if(calendar.before(current))
{
calendar.add(Calendar.DATE, 1);
}
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
}
}
}
The service with notification builder:
public class QuoteOfTheDayIntentService extends IntentService {
private static final int NOTIFICATION_ID = 3;
public QuoteOfTheDayIntentService() {
super("MyNewIntentService");
}
#Override
protected void onHandleIntent(Intent intent) {
Notification.Builder builder = new Notification.Builder(this);
builder.setContentTitle(getString(R.string.quote_of_the_day));
DatabaseHandler db = DatabaseHandler.getInstance(this);
Random generator = new Random();
int id = generator.nextInt(db.getQuotesSize());
Quote quote = db.getQuote(id, null);
builder.setContentText(quote.getQuote());
builder.setSmallIcon(R.drawable.swords);
builder.setAutoCancel(true);
Intent notifyIntent = new Intent(this, MainActivity.class);
notifyIntent.putExtra("id",id-1);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
Notification notificationCompat = builder.build();
NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);
managerCompat.notify(NOTIFICATION_ID, notificationCompat);
}
}

Receiver not calling in Alarm Manager android

Hi I need to use notification for particular date in android. So I used alarm manager to trigger in receiver. But Broadcast receiver is not calling. Here is my code.
Here is piece of code..
In Details activity:
Log.e("alarm set", "cal" + cal.getTime());
setAlarm(cal);
private void setAlarm(Calendar targetCal){
//this log got printed.
Log.e("alarm set",""+targetCal.getTime());
Intent intent = new Intent(Details.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Details.this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
}
But AlarmReceiver is not calling.
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent arg1) {
//this is not working.
Log.e("alarm",";alarm");
Toast.makeText(context, "Alarm received!", Toast.LENGTH_LONG).show();
Intent service1 = new Intent(context, AlarmService.class);
context.startService(service1);
}
}
I given permission as,
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<receiver
android:name=".AlarmReceiver"
android:process=":remote" />
<service
android:name=".AlarmService"
android:enabled="true"
android:exported="true"></service>
I couldn't find what mistake I am doing.
Thanks in advance.

How to trigger Notification in background?

I'm trying to trigger a notification every 15 minutes, starting from the time I set. Currently my app notifies only when my application is open or when it is in the recent applications but when i closed my application the notification doesn't work any longer but when i open again my app it will trigger the notification. Based on my research I need a Service. So I have 3 files.
In my MainActivity, i set the alarm
private void scheduleNotification(int week){
Intent notificationIntent = new Intent(this, NotificationPublisher.class);
notificationIntent.putExtra("notifId", getResources().getInteger(R.integer.notification_id));
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, getResources().getInteger(R.integer.notification_id), notificationIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, week);
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent);
}
in the MainActivity i call a BroadcastReceiver which is the NotificationPublisher. Here is my NotificationPublisher
public class NotificationPublisher extends BroadcastReceiver {
final String TAG = "NotificationPublisher";
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, NotificationAlarmService.class);
service.putExtra("notifId", intent.getIntExtra("notifId", 0));
context.startService(service);
}
then I start a service by using the BroadcastReceiver which is the NotificationAlarmService, here i build a Notification using NotificationCompat.Builder.
public class NotificationAlarmService extends Service {
. . . .
#Override
public int onStartCommand(Intent intent,int flag, int startId)
{
notificationManager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent mIntent = new Intent(this, MainActivity.class);
pendingIntent = PendingIntent.getActivity(this, intent.getIntExtra("notifId", 0), mIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("App");
builder.setContentText("Notify Me");
builder.setAutoCancel(true);
builder.setSmallIcon(getNotificationIcon());
builder.setContentIntent(pendingIntent);
notificationManager.notify(intent.getIntExtra("notifId", 0), builder.build());
return super.onStartCommand(intent, flag, startId);
}
I already tried returning START_STICKY instead of super.onStartCommand I understand that when i used a service it will run in the background even if the user closed the application. I also tried changing this and used getBaseContext() in the service but this it doesn't trigger the notification. I don't know if i'm missing something. Thank you in advance.
EDIT
Here is my Manifest
<receiver android:name=".NotificationPublisher"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<service android:name=".NotificationAlarmService"
android:enabled="true">
</service>
After a week, I finally got it. In my Manifest i put something like this
<receiver android:name=".NotificationPublisher">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
<service android:name=".NotificationAlarmService"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="NOTIFICATION_SERVICE" />
</intent-filter>
</service>
i've removed the enabled="true" in the receiver and also add QUICKBOOT_POWERON. Then add an action in the service which is NOTIFICATION_SERVICE and call it in my service like this
NotificationManager mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Try adding Service in your Manifest file (Same as declaring an Activity) under application tag like this,
<service android:name="com.example.android.NotificationAlarmService">
</service>

broadcast receiver not getting called with alarm

I'm trying to call a broadcast receiver at a certain time by setting an alarm. It doesn't appear to be getting called at all. I'm trying to debug with logcat.
My set up for the alarm is something like this:
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar notifyAlarm = Calendar.getInstance();
notifyAlarm.set(Calendar.HOUR, 17);
notifyAlarm.set(Calendar.MINUTE, 00);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 2378530, intent, 0);
alarm.set(AlarmManager.RTC_WAKEUP, notifyAlarm.getTimeInMillis(), pendingIntent);
My receiver is as follows:
public class AlarmReceiver extends BroadcastReceiver {
private static final String TAG ="test receiver";
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.v(TAG, "receiver called");
}
}
also in the manifest I have:
</activity>
<receiver android:name="AlarmReceiver"></receiver>
</application>
Any help appreciated.
Thinking about it, I'm not sure you can send a broadcast to a BroadcastReceiver using an explicit Intent as you are doing it...
Intent intent = new Intent(this, AlarmReceiver.class);
Try giving the <receiver section an <intent-filter> example...
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="com.mypackage.ACTION_DO_SOMETHING" />
</intent-filter>
</receiver>
Then when you create your Intent, do it as follows...
Intent intent = new Intent("com.mypackage.ACTION_DO_SOMETHING");

Categories

Resources