Unable to show notification when app is closed - android

I am trying to show notification at specific date I am using alarm manager and broadcast receiver to show notifications but the problem is notification works only when app is open and when app is closed notification does not show. Below is my code:
Reminder.java
public class Reminder extends AppCompatActivity {
long reminderDateTimeInMilliseconds = 000;
Button but;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reminder);
but = findViewById(R.id.but);
but.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
createNotifyChannel();
Intent intent = new Intent(Reminder.this,ReminderBroadcast.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Reminder.this,0,intent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Calendar calendarToSchedule = Calendar.getInstance();
calendarToSchedule.setTimeInMillis(System.currentTimeMillis());
calendarToSchedule.clear();
//.Set(Year, Month, Day, Hour, Minutes, Seconds);
calendarToSchedule.set(2020, 8, 20, 19, 12, 0);
reminderDateTimeInMilliseconds = calendarToSchedule.getTimeInMillis();
alarmManager.setExact(AlarmManager.RTC_WAKEUP,reminderDateTimeInMilliseconds,pendingIntent);
}
});
}
private void createNotifyChannel(){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
CharSequence name = "ReminChannel";
String desc = "This is my channel";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("mynotif",name,importance);
channel.setDescription(desc);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}
ReminderBroadcast.java
public class ReminderBroadcast extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
NotificationCompat.Builder notif = new NotificationCompat.Builder(context,"mynotif")
.setContentTitle("Appointment reminder")
.setContentText("Hello there")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat manager = NotificationManagerCompat.from(context);
manager.notify(200,notif.build());
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.firstapp">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Reminder">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ThirdActivity" />
<activity android:name=".SecondActivity" />
<activity android:name=".MainActivity">
</activity>
<receiver android:name=".ReminderBroadcast"/>
</application>
</manifest>
What am I doing wrong?

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>

Alarm Manager Example does not work

I am trying to follow the example given here on running some code in fixed time intervals. I used the code in this example (setting the waiting time to 1 minute instead), compiled it, started in on my phone, stopped the application on the phone, and waited for several minutes.
However, no Toast was shown and neither any log-entry. I also notice that the Service.onStart seems depreciated.
Is something missing in the example? Do I need to register something when the main activity of the app is started? Or call it from somewhere else in my app? Or maybe something is wrong in the manifest?
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mypackage.test" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application>
android:allowBackup="true"
android:icon="#mipmap/stoxx"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat.Light" >
<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=".NewEntryActivity"
android:label="#string/menu_add"
android:theme="#style/AppTheme.NoActionBar"
/>
<activity
android:name=".UserSettingActivity"
android:label="#string/menu_add"
android:theme="#style/AppTheme.NoActionBar"
/>
<receiver
android:process=":remote"
android:name=".Alarm">
</receiver>
</application>
</manifest>
You forgot to include service in your Manifest. Here is all this project that works for me, check it.
Here is my manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.alarmmanager.just_me.alarmmanager">
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application android:allowBackup="true" android:label="#string/app_name"
android:icon="#mipmap/ic_launcher" android:supportsRtl="true"
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:process=":remote" android:name=".Alarm"/>
<service android:name=".MyService"/>
</application>
Here is Alarm:
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, "com.alarmmanager.just_me.alarmmanager.Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example
wl.release();
}
public void SetAlarm(Context context)
{
AlarmManager am =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, Alarm.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 5, pi); // Millisec * Second
}
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);
}}
Here is service:
public class MyService extends Service
{
Alarm alarm = new Alarm();
public void onCreate()
{
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.d("!~!", "Service started.");
alarm.SetAlarm(this);
return START_STICKY;
}
#Override
public void onStart(Intent intent, int startId)
{
alarm.SetAlarm(this);
}
#Override
public IBinder onBind(Intent intent)
{
return null;
}
}
And the last one MainActivity:
public class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MyService.class);
startService(intent);
}
}

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>

Why does my BroadcastReceiver Doesn't work properly?

I'm stuck in this problem for 3 days and I'm so frustrated ..
I don't know why on earth my BroadCastReceiver doesn't work ...
I'll sincerely appreciate solving my problem...
Here is my code..
MainActivity (I posted some pieces of codes from my whole codes.. that I regard as important to understand)
This MainActivity, I get AlarmManager..
:
public class MainActivity extends FragmentActivity implements TabListener{
private GregorianCalendar mCalendar;
private NotificationManager mNotification;
private AlarmManager mManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNotification = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
//Get AlarmManager here..
mManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
....
..
}
public NotificationManager getNotification(){
return mNotification;
}
public AlarmManager getAlarmManager(){
return mManager;
}
And here is my MsgBookingFragment(this class extends Fragment..) :
//...somewhere else of my codes..
//Access to SQLDataBase and get Date, and set it in bookDate..
bookDate.set(Integer.parseInt(year.split("년")[0]),
Integer.parseInt(month.split("월")[0]),
Integer.parseInt(day.split("일")[0]),
Integer.parseInt(hourOfDay.split("시")[0]),
Integer.parseInt(minute.split("분")[0]));
//get mainActivity, in order to get AlarmManager..
final MainActivity mainActivity = (MainActivity)getActivity();
AlertDialog.Builder alert_confirm = new AlertDialog.Builder(getActivity());
alert_confirm.setMessage("예약 하시겠습니까?").setCancelable(false).setPositiveButton("예",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
doSomething();
msgViewService.write(vo);
Log.i("MsgBookingfragment", "msgViewInputVO write done = " + vo.toString());
FragmentManager FM = getFragmentManager();
FM.popBackStack();
FragmentTransaction ft = FM.beginTransaction();
mainActivity.setHashMapInsert(vo.getPersonID());
mainActivity.setBookedHashInsert(vo.getPersonID());
ft.replace(R.id.findperson, new FindPersonFragment()).commit();
//here, I get AlarmSet Instance and setAlarm..!!
AlarmSet alarm = new AlarmSet(bookDate,
getActivity().getApplicationContext(),
((MainActivity)getActivity()).getAlarmManager(),
((MainActivity)getActivity()).getNotification() );
alarm.setAlarm();
Toast.makeText(getActivity().getApplicationContext(), "예약되었습니다.", Toast.LENGTH_LONG).show();
}
And here is my AlarmSet Class :
public class AlarmSet extends Activity implements OnDateChangedListener, OnTimeChangedListener{
private String tag = "AlarmSet";
private Calendar bookDate = Calendar.getInstance();
private Context context;
private AlarmManager mManager;
private NotificationManager mNotification;
public AlarmSet(Calendar cal, Context c, AlarmManager am, NotificationManager noti){
bookDate = cal;
context = c;
mNotification = noti;
mManager = am;
}
public void setAlarm() {
mManager.set(AlarmManager.RTC_WAKEUP, bookDate.getTimeInMillis(), pendingIntent());
Log.i("setAlarm : ", bookDate.getTime().toString());
//Log shows this message : Sat Oct 18 14:25:52 GMT+09:00 2014
}
//set free Alarm
public void resetAlarm() {
mManager.cancel(pendingIntent());
}
public PendingIntent pendingIntent() {
Intent i = new Intent(context, AlarmReceiver.class);
PendingIntent pi = PendingIntent.getActivity(context, 3 , i, PendingIntent.FLAG_CANCEL_CURRENT);
Log.i(tag, "PendingIntent.......");
return pi;
}
here is my AlarmReceiver :
public class AlarmReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i("TAg", "ggggggggggggggggggggg");
Toast.makeText(context, R.string.app_name, Toast.LENGTH_SHORT).show();
}
And here is my ManiFast :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.dduo.hrelation"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name="org.dduo.hrelation.AlarmReceiver">
<intent-filter>
<action android:name="action"/>
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:windowSoftInputMode="adjustResize"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
So long pieces of codes...
By the way, I reckon it's so doubtful of pendingIntent method... I guess the problem is because of that method.. I've never seen that "ggggggggg" log msg.. and toast msg on my test device..
Please... help me..
in ManiFast :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.dduo.hrelation"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
//---> declare broadcast receiver like this
<receiver android:name="org.dduo.hrelation.AlarmReceiver">
</receiver>
<activity
android:name=".MainActivity"
android:windowSoftInputMode="adjustResize"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
and pendingIntent():
public PendingIntent pendingIntent() {
Intent i = new Intent(context, AlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 0, i, 0);
Log.i(tag, "PendingIntent.......");
return pi;
}

Categories

Resources