Alarmmanager for a notification doesn't work - android

I've got an alarmmanager to display a notification at a certain date and time.
I schedule a notification like this:
private void scheduleNotification(String title, String text, String subtext, long futureInMillis) {
Intent notificationIntent = new Intent(getActivity(), NotificationPublisher.class);
notificationIntent.putExtra("title", title);
notificationIntent.putExtra("text", text);
notificationIntent.putExtra("subtext", text);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
}
The broadcastreceiver:
public class NotificationPublisher extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Log.v("receiver", "notification received");
String title = intent.getStringExtra("title");
String text = intent.getStringExtra("text");
String subtext = intent.getStringExtra("subtext");
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSmallIcon(R.drawable.checkbox_on_24dp);
builder.setContentTitle(title);
builder.setContentText(text);
builder.setSubText(subtext);
builder.setSound(uri);
builder.setVibrate(new long[]{1000});
Notification not = builder.build();
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, not);
}
}
The manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.christophbielen.simplytodo">
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name=".data.DataProvider"
android:authorities="com.christophbielen.simplytodo" />
<activity
android:name=".DetailActivity"
android:label="#string/title_activity_detail"
android:parentActivityName=".MainActivity"
android:theme="#style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.christophbielen.simplytodo.MainActivity" />
</activity>
<activity
android:name=".ListCategoriesActivity"
android:label="#string/title_activity_list_categories"
android:parentActivityName=".MainActivity"
android:theme="#style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.christophbielen.simplytodo.MainActivity" />
</activity>
<activity
android:name=".AddListActivity"
android:label="#string/title_activity_add_list"
android:parentActivityName=".MainActivity"
android:theme="#style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.christophbielen.simplytodo.MainActivity" />
</activity>
<activity
android:name=".EditListActivity"
android:label="#string/title_activity_edit_list"
android:parentActivityName=".MainActivity"
android:theme="#style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.christophbielen.simplytodo.MainActivity" />
</activity>
<activity
android:name=".AddItemActivity"
android:label="#string/title_activity_add_item"
android:theme="#style/AppTheme.NoActionBar">
<!--<meta-data-->
<!--android:name="android.support.PARENT_ACTIVITY"-->
<!--android:value="com.christophbielen.simplytodo.MainActivity" />-->
</activity>
<activity
android:name=".EditItemActivity"
android:label="#string/title_activity_edit_item"
android:theme="#style/AppTheme.NoActionBar">
</activity>
<receiver android:name=".NotificationPublisher" />
</application>
And I call schedule Notification like this:
int year = Integer.parseInt(date.substring(0,4));
int month = Integer.parseInt(date.substring(4,6))-1;
int day = Integer.parseInt(date.substring(6,8));
int hour = Integer.parseInt(time.substring(0,2));
int minute = Integer.parseInt(time.substring(2));
Calendar cal=Calendar.getInstance();
cal.set(Calendar.MONTH,month);
cal.set(Calendar.YEAR,year);
cal.set(Calendar.DAY_OF_MONTH,day);
cal.set(Calendar.HOUR_OF_DAY,hour);
cal.set(Calendar.MINUTE,minute);
cal.set(Calendar.SECOND,0);
String subcontent = catName + " due on: " + year + "/" + month+1 + "/" + day + " " + hour + ":" + minute;
scheduleNotification("To do!", shoppingListName, subcontent, cal.getTimeInMillis());
When I schedule a notification it seems to work but the receiver never receives anything.
I looked everywhere for an answer and I can't figure out why the alarmmanager doesn't work.
Thanks

Please Use -
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, (System.currentTimeMillis() + futureInMillis, pendingIntent);
instead of -
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);

When you schedule an alarm with ELAPSED_REALTIME_WAKEUP, you must use SystemClock.elapsedRealtime() as the base offset for the time.
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + futureInMillis, pendingIntent);
DroidFiddle example: https://droidfiddle.net/vwkttsz/1

Related

Notification click is not working

i m generating notification.so i created the notification and i m getting the notification when i click the button.now,i created onclick function and some toast,but when i clicked on thone button in notificatio i m getting toast message. i dont know whats wrong in below code.so please help.thanks in adavance.
this is my broadcast activity
public class NotificationBroadcast extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(NotificationGenerator.NOTIFY_PLAY))
{
Toast.makeText(context,"NOTIFY PLAY",Toast.LENGTH_LONG).show();
}
if(intent.getAction().equals(NotificationGenerator.NOTIFY_PAUSE))
{
Toast.makeText(context,"NOTIFY PAUSE",Toast.LENGTH_LONG).show();
}
if(intent.getAction().equals(NotificationGenerator.NOTIFY_NEXT))
{
Toast.makeText(context,"NOTIFY NEXT",Toast.LENGTH_LONG).show();
}
if(intent.getAction().equals(NotificationGenerator.NOTIFY_DELETE))
{
Toast.makeText(context,"NOTIFY DELETE",Toast.LENGTH_LONG).show();
}
if(intent.getAction().equals("com.example.murarilal.atry.previous"))
{
Toast.makeText(context,"NOTIFY PREVIOUS",Toast.LENGTH_LONG).show();
}
}
}
this is notifiction builder activity
public class NotificationGenerator{
public static final String NOTIFY_PREVIOUS="com.example.murarilal.atry.previous";
public static final String NOTIFY_DELETE="com.example.murarilal.atry.delete";
public static final String NOTIFY_PAUSE="com.example.murarilal.atry.pause";
public static final String NOTIFY_PLAY="com.example.murarilal.atry.play";
public static final String NOTIFY_NEXT="com.example.murarilal.atry.next";
private static final int NOTIFICATION_ID_OPEN_ACTIVITY=1;
private static String ID = "default";
public static void openActivityNotification(Context context) {
RemoteViews expandView=new RemoteViews(context.getPackageName(),R.layout.notification);
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";
int requestID = (int) System.currentTimeMillis();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_MAX);
// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context);
notificationBuilder.setChannelId(ID);
Intent notifyIntent=new Intent(context,BlankFragment2.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent=PendingIntent.getActivity(context,requestID,notifyIntent,PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(pendingIntent);
Intent intent = new Intent(context, BlankFragment2.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(intent);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ed)
.setTicker("Hearty365")
// .setPriority(Notification.PRIORITY_MAX)
.setContentTitle("try")
.setCustomBigContentView(expandView)
.setContentText("notification")
.setContentInfo("Info");
notificationManager.notify(NOTIFICATION_ID_OPEN_ACTIVITY, notificationBuilder.build());
}
public static void customBigNotification(Context context)
{
int requestID = (int) System.currentTimeMillis();
RemoteViews expandView=new RemoteViews(context.getPackageName(),R.layout.notification);
NotificationCompat.Builder nc=new NotificationCompat.Builder(context);
NotificationManager nm=(NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
Intent notifyIntent=new Intent(context,BlankFragment2.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent=PendingIntent.getActivity(context,requestID,notifyIntent,PendingIntent.FLAG_UPDATE_CURRENT);
nc.setContentIntent(pendingIntent);
nc.setSmallIcon(R.drawable.ic_play_circle_filled_black_24dp);
nc.setAutoCancel(true);
nc.setCustomBigContentView(expandView);
nc.setContentTitle("music player");
nc.setContentText("control audio");
nc.getBigContentView().setTextViewText(R.id.songName,"shape of you");
setListeners(expandView,context);
}
private static void setListeners(RemoteViews views, Context context)
{
int requestID = (int) System.currentTimeMillis();
Intent previous= new Intent(NOTIFY_PREVIOUS);
Intent next=new Intent(NOTIFY_NEXT);
Intent delete=new Intent(NOTIFY_DELETE);
Intent play=new Intent(NOTIFY_PLAY);
Intent pause=new Intent(NOTIFY_PAUSE);
PendingIntent pPrevious=PendingIntent.getBroadcast(context,requestID,previous,PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.previous,pPrevious);
PendingIntent pDelete=PendingIntent.getBroadcast(context,requestID,delete,PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.cancel,pDelete);
PendingIntent pNext=PendingIntent.getBroadcast(context,requestID,next,PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.next,pNext);
PendingIntent pPlay=PendingIntent.getBroadcast(context,requestID,play,PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.play,pPlay);
PendingIntent pPause=PendingIntent.getBroadcast(context,requestID,pause,PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.play,pPause);
}
}
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.murarilal.atry">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<permission android:name="android.permission.MEDIA_CONTENT_CONTROL" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="#style/mytheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".screen"
android:label="#string/title_activity_screen"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".com.layout.blankFragment2"
android:label="#string/hello_blank_fragment"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name="com.exlayout.albumSongs"
android:label="#string/title_activity_album_songs"
android:theme="#android:style/Theme.Holo.Light.DarkActionBar" />
<activity
android:name="com.exlayout.Main2Activity"
android:label="#string/title_activity_main2"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".Main2Activity"
android:label="#string/title_activity_main2"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".albumSongs"
android:label="#string/title_activity_album_songs"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".albumSong"
android:label="#string/title_activity_album_song"
android:theme="#android:style/Theme.Holo.Light.DarkActionBar" />
<activity
android:name=".BlankFragment2"
android:label="#string/hello_blank_fragment"
android:theme="#style/AppTheme.NoActionBar" />
<receiver android:name=".NotificationBroadcast">
<intent-filter>
<action android:name="com.example.murarilal.atry.previous" />
<action android:name="com.example.murarilal.atry.delete" />
<action android:name="com.example.murarilal.atry.pause" />
<action android:name="com.example.murarilal.atry.next" />
<action android:name="com.example.murarilal.atry.play" />
</intent-filter>
</receiver>
<activity
android:name=".genreSongs"
android:label="#string/title_activity_genre_songs"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name="com.exlayout.genreSongs"
android:label="#string/title_activity_genre_songs"
android:theme="#android:style/Theme.Holo.Light.DarkActionBar" />
<activity
android:name=".artist_Songs"
android:label="#string/title_activity_artist__songs"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name="com.exlayout.artist_Songs"
android:label="#string/title_activity_artist__songs"
android:theme="#android:style/Theme.Holo.Light.DarkActionBar" />
<activity
android:name=".checkActivity"
android:label="#string/title_activity_check"
android:theme="#style/Theme.AppCompat.Light.NoActionBar" />
<activity
android:name=".favoutitesActivity"
android:label="#string/title_activity_favoutites"
android:theme="#style/AppTheme" />
<activity
android:name=".recent"
android:label="#string/title_activity_recent"></activity>
</application>
</manifest>

I can't controll the setting time in AlarmManager when restart APP

I want to set the notification for custom time , i take a reference from http://www.singhajit.com/schedule-local-notification-in-android/
I add some manifest code which is for when the device reboot.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:installLocation="internalOnly"
package="com.example.user.testcounddown">
<!-- for reboot---------------------- -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="android.media.action.DISPLAY_NOTIFICATION" />
<category android:name="android.intent.category.DEFAULT" />
<!-- for reboot--------------------- -->
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>
The problem is when i reboot my device , the notification shows immediately , not after about 300esc cal.add(Calendar.SECOND, 300);
here is my MainActivity code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
notificationIntent.addCategory("android.intent.category.DEFAULT");
PendingIntent broadcast = PendingIntent.getBroadcast(this, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 300);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);//for API<=18
}
my receiver code:
public class AlarmReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Intent notificationIntent = new Intent(context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Notification notification = builder.setContentTitle("Demo App Notification")
.setContentText("New Notification From Demo App..")
.setTicker("New Message Alert!")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent).build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}
}
How do i let the notification shows in the right time when device reboot ?
cal.add(Calendar.SECOND, 300);
Any help would be greatly appreciated,thanks.

notification in second activity not working

i have posted a question about this an the solution was perfect , but when i merge it with my code there is no result , the app dont make the notification ,
like here i put " mo " as the start activity .
here is my code :
public class mo extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mo);
}
and the second was which has the alarm manager :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
notificationIntent.addCategory("android.intent.category.DEFAULT");
PendingIntent broadcast = PendingIntent.getBroadcast(this, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// Calendar cal = Calendar.getInstance();
Calendar calendar = Calendar.getInstance();
// Settings calendar for 01/05/2016 12:33:00 AM
calendar.set(Calendar.YEAR, 2016);
calendar.set(Calendar.MONTH, 2); // January has value 0
calendar.set(Calendar.DAY_OF_MONTH, 15);
calendar.set(Calendar.HOUR_OF_DAY, 1);
calendar.set(Calendar.MINUTE, 18);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM, Calendar.PM );
// cal.add(Calendar.SECOND, 15);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), broadcast);
The broadcastReceiver :
public class AlarmReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Intent notificationIntent = new Intent(context, NotificationActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(NotificationActivity.class);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Notification notification = builder.setContentTitle(" App Notification")
.setContentText("New Notification From Demmmmo App..")
.setTicker("New Message Alert!")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent).build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}
}
The notificationActivity :
public class NotificationActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notification_activity);
}
}
and also here the manifest :
<?xml version="1.0" encoding="utf-8"?>
<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" >
</activity>
<activity android:name=".NotificationActivity" />
<receiver android:name=".AlarmReceiver" >
<intent-filter>
<action android:name="android.media.action.DISPLAY_NOTIFICATION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity
android:name=".mo"
android:label="#string/title_activity_mo" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.media.action.IMAGE_CAPTURE" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.media.action.IMAGE_CAPTURE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>

Android - alarm notification issue

I'm trying to configure an alarm notification in Android and unfortunately no alarm is generated.
Here is the creation of the alarm:
myDate = new SimpleDateFormat("dd/MM/yyyy HH:mm").parse(time_Date_str);
t.setDueDate(myDate);
t.setHasDate(true);
Intent alarmNotificationIntent = new Intent(this, ReminderNotification.class);
alarmNotificationIntent.putExtra("task", t);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(this, (int) task_id, alarmNotificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(t.getDueDate().getTime());
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
In addition I've created:
NotificationReceiver
import android.app.Activity;
import android.os.Bundle;
public class NotificationReceiver extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
}
ReminderNotification
import android.content.Context;
import android.content.Intent;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
public class ReminderNotification extends BroadcastReceiver
{
public ReminderNotification()
{
// TODO Auto-generated constructor stub
}
#Override
public void onReceive(Context context, Intent intent) {
// The PendingIntent to launch our activity if the user selects this notification
Task task = (Task)intent.getSerializableExtra("task");
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
// Set the info for the views that show in the notification panel.
Intent snzInt = new Intent(context, SnoozeReminderReceiver.class);
snzInt.putExtra("task", task);
PendingIntent snoozeIntent = PendingIntent.getBroadcast(context, 0,snzInt, PendingIntent.FLAG_CANCEL_CURRENT);
Intent doneInt = new Intent(context,DoneActionReceiver.class);
doneInt.putExtra("task", task);
PendingIntent doneIntent = PendingIntent.getBroadcast(context, 0,doneInt, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager notificationManager =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
}
}
The manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="il.ac.she.dd.todoli"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<receiver android:name="ReminderNotification"/>
<receiver android:name="SnoozeReminderReceiver"/>
<receiver android:name="DoneActionReceiver"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".ListNodeActivity"
android:label="#string/title_activity_list_node"
android:parentActivityName=".MainActivity"
android:theme="#style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="il.ac.shenkar.david.todolistex2.MainActivity" />
</activity>
<activity
android:name=".Signup_Activity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar"></activity>
<activity
android:name=".create_team"
android:label="Create new task team"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".invite_member"
android:label="Invite Members Your Team"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".Login_activity"
android:label="Login to Wiggle"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".SplashScreen"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".EditTaskActivity"
android:label="Edit Task"
android:theme="#style/AppTheme.NoActionBar"></activity>
</application>
I have a feeling that I'm missing something very small but cannot find the issue.
Please let me know what I'm doing wrong.
Thanks in advance.
you have to first build notification and then notify it...
NotificationManager _manager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification.Builder(this)
.setContentTitle("New")
.setContentText("hello")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pintent)
.build();
notification.vibrate=new long[]{100,250,100,500};
_manager.notify(notification_id, notification);
You can find a complete example of Alarm and Notification in my this GitHub Repo

Notification manager not working with alarmManager

I am trying to use alarm manager to show notification after 5 sec. I have tried many sites but was not able to understand please give a simple example for explaining how to use alarm manager and connect notification with it.
I am newbie.
this is function i used to set alarm and I am not getting notification after 5 secs actually not at all not in emulator nor in android mobile. If put code to create notififcation with a button pressing that is working great.
public void setAlarm(View view)
{
Intent alertIntent = new Intent(this, AlertReciver.class);
Long alertTime = new GregorianCalendar().getTimeInMillis()+5*1000;
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime, PendingIntent.getBroadcast(this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
tv.setText("completed");
}
and this class to make it work
public class AlertReciver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
createNotification(context,"times Up", "5 SEcond has passed", "Alert");
}
public void createNotification(Context context,String msg, String msgText, String msgAlert){
PendingIntent notificIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ne)
.setContentTitle(msg)
.setTicker(msgAlert)
.setContentText(msgText);
mBuilder.setContentIntent(notificIntent);
mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
this is the manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.deepaksingh.goinnotifying" >
<uses-permission android:name="android.permission.alarm.permission.SET_ALARM" />
<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>
<activity
android:name=".MoreInfoNotification"
android:label="#string/title_activity_more_info_notification" >
<meta-data
android:name="android support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</application>
You havn't set permission for wake:
<uses-permission android:name="android.permission.WAKE_LOCK" />
ANd you didn't register your recevier.Here.
<?xml version="1.0" encoding="utf-8"?>
<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>
<activity
android:name=".MoreInfoNotification"
android:label="#string/title_activity_more_info_notification" >
<meta-data
android:name="android support.PARENT_ACTIVITY"
android:value=".MainActivity" />
<receiver android:name=".AlertReciver "/>
</application>

Categories

Resources