Skip to content Skip to sidebar Skip to footer

Alarm With Notification, Notification Wont Pop Up

So I don't know why this code is not working. I wanted to make an 'alarm' notification that would go off once a day. Just wanted to say I'm new to android. Changed up the code a bi

Solution 1:

You are creating the PendingIntent with PendingIntent.getService(), but you are supplying an Intent with an Activity.

So for your code to work you would need to use PendingIntent.getActivity(). (Additionally you have to add the corresponding <activity> tag to your AndroidManifest.xml.)

However, this is probably not exactly what you want: The only thing the lol Activity does is adding a notification. You might want to use a BroadcastReceiver (or perhaps a WakefulBroadcastReceiver) instead. In this case you would need to use PendingIntent.getBroadcast() instead of getActivity().

Solution 2:

Follow these steps

1Create an Activity and add this code to setAlarm(can be any name)

Suppose you want to set Alarm for 24/02/15 ,14:00PM

@SuppressLint("UseSparseArrays")
publicvoidsetAlarms(Long id,String event_date,String event_time,Context context)
{



    Calendar cal=Calendar.getInstance();
    String[] parts=event_date.split("/");

    String Event_date=parts[0];
    String Event_month=parts[1];
    String Event_Year=parts[2];

    String[] parts1=event_time.split(":");

    String Event_HOD=parts1[0];
    String Event_MIN=parts1[1];

    cal.add(Calendar.YEAR, Integer.parseInt(Event_Year)-cal.get(Calendar.YEAR));
    cal.add(Calendar.MONTH, (Integer.parseInt(Event_month)-1)-cal.get(Calendar.MONTH));
    cal.add(Calendar.DAY_OF_MONTH, Integer.parseInt(Event_date)-cal.get(Calendar.DAY_OF_MONTH));
    cal.add(Calendar.HOUR_OF_DAY, Integer.parseInt(Event_HOD)-cal.get(Calendar.HOUR_OF_DAY));
    cal.add(Calendar.MINUTE, Integer.parseInt(Event_MIN)-cal.get(Calendar.MINUTE));
    cal.add(Calendar.SECOND, 0);


    AlarmManager am =(AlarmManager)context.getSystemService(Activity.ALARM_SERVICE);

     //Your BroadcastReceiver which will receive Alarm
    Intent intent = new Intent(context, AlarmReceiver.class);

    //you can use PutExtra() to send additional parameter with Intent//PendingIntent to add
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
        (int)(long)id, intent, PendingIntent.FLAG_UPDATE_CURRENT);


        am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                pendingIntent);

 Toast.makeText(context, "alarm Set for Know", Toast.LENGTH_LONG).show();
 }

2)Inside BroadcastReceiver onReceive() method

@OverridepublicvoidonReceive(Context context, Intent intent) {

    //Receive parameter and do what you want to do with parameter//Using RemoteViews you can Inflate a CustomLayout when user clicks      on Notification when you do not want any CustomLayout for Notification remove itRemoteViewscontentView1=newRemoteViews("com.example.new_reminder_book" , R.layout.custom_notification);
     contentView1.setTextViewText(R.id.cn_tv_title, Event_title);
        contentView1.setTextViewText(R.id.cn_tv_category, Event_category);
        contentView1.setImageViewResource(R.id.cn_tv_image, R.drawable.event);


    NotificationCompat.Builder mBuilder=newNotificationCompat.Builder(context)
    .setSmallIcon(R.drawable.appicon)
    .setContent(contentView1)
    .setAutoCancel(true)
    .setOngoing(true);


    //Target Activity that will start when you click on Notification
    Intent result=newIntent(context, EmptyActivity.class);


    // The stack builder object will contain an artificial back stack for the// started Activity.// This ensures that navigating backward from the Activity leads out of// your application to the Home screen.TaskStackBuilderstackBuilder= TaskStackBuilder.create(context);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(EmptyActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(result);

    PendingIntentresultPendingIntent=
            stackBuilder.getPendingIntent(
                0,
                PendingIntent.FLAG_UPDATE_CURRENT
            );

    mBuilder.setContentIntent(resultPendingIntent);

    Notification notification=mBuilder.build();
    notification.contentView=contentView1;



    NotificationManagermNotificationManager=
        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
    mNotificationManager.notify((int) System.currentTimeMillis(), notification);

    //A mp3 fine in res->raw that will play when notification fireMediaPlayermp= MediaPlayer.create(context, R.raw.listen);
    mp.start();

    PowerManagerpm= (PowerManager)     context.getSystemService(Context.POWER_SERVICE);

    PowerManager.WakeLockwl= pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");

    wl.acquire();

    /*Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);

     //Set pattern for Vibration
    long[] pattern={2000,3000,4000};

    vibrator.vibrate(pattern,1);*/

    wl.release();


}

}

3)Declare Two permissions in Manifest.xml

<uses-permissionandroid:name="android.permission.WAKE_LOCK"/><uses-permissionandroid:name="android.permission.VIBRATE"/>

4)Declare You AlarmReceiver in Manifest.xml

<receiverandroid:name=".extra.AlarmReceiver"android:label="@string/title_activity_alarm_receiver"android:exported="true"></receiver>

Follow these documents http://developer.android.com/reference/android/content/BroadcastReceiver.htmlhttp://developer.android.com/reference/android/app/AlarmManager.htmlhttp://developer.android.com/guide/topics/ui/notifiers/notifications.htmlhttp://developer.android.com/reference/android/os/PowerManager.WakeLock.html

Hope this will help you

Post a Comment for "Alarm With Notification, Notification Wont Pop Up"