Skip to content

Instantly share code, notes, and snippets.

@cleidimarviana
Last active September 3, 2020 23:59
Show Gist options
  • Save cleidimarviana/70322528a9f60e57d1e70685e561b11c to your computer and use it in GitHub Desktop.
Save cleidimarviana/70322528a9f60e57d1e70685e561b11c to your computer and use it in GitHub Desktop.
import android.app.AlarmManager;
import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.support.v7.app.NotificationCompat;
import com.acklay.frazy.MainActivity;
import com.acklay.frazy.R;
import java.util.Calendar;
public class AlarmBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 16);
calendar.set(Calendar.MINUTE, 13);
calendar.set(Calendar.SECOND, 0);
Intent serviceIntent = new Intent(context, NotificationService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, serviceIntent, 0);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY,
pendingIntent);
}
}
public class NotificationService extends IntentService {
public NotificationService() {
super("NotificationService");
}
@Override
protected void onHandleIntent(Intent intent) {
sendNotification(getApplicationContext());
}
/**
* Send simple notification using the NotificationCompat API.
*/
public void sendNotification(Context context) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setSmallIcon(R.mipmap.ic_launcher);
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
builder.setContentIntent(pendingIntent);
builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher));
builder.setContentTitle("Titulo da notificação");
builder.setContentText("O conteudo da notificaçãoooo estará aqui");
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
// Will display the notification in the notification bar
notificationManager.notify(1, builder.build());
}
}
}
<receiver android:name=".AlarmBroadcast">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment