简体   繁体   English

简单重复推送通知Android

[英]Simple Repeating Push Notification Android

I want to create a simple push notification for my Android app (repeating every day on 6pm) 我想为我的Android应用创建一个简单的推送通知(每天下午6点重复)

I did some search and found: How To give notifications on android on specific time? 我做了一些搜索,发现: 如何在特定时间在android上发出通知?

I followed that tutorial and here is what I got so far: 我遵循了该教程,这是到目前为止的内容:

public class MyReceiver extends BroadcastReceiver
{
public MyReceiver()
{

}
@Override
public void onReceive(Context context, Intent intent)
{
    Intent intent1 = new Intent(context, HistoryToday.class);
    context.startService(intent1);
}
}


public class MyNewIntentService extends IntentService

{ private static final int NOTIFICATION_ID = 3; {private static final int NOTIFICATION_ID = 3;

public MyNewIntentService() {
    super("MyNewIntentService");
}

@Override
protected void onHandleIntent(Intent intent) {
    Notification.Builder builder = new Notification.Builder(this);
    builder.setContentTitle("My Titel");
    builder.setContentText("This is the Body");
    builder.setSmallIcon(R.mipmap.logo_app_icon);
    Intent notifyIntent = new Intent(this, HistoryToday.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 2, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    //to be able to launch your activity from the notification
    builder.setContentIntent(pendingIntent);
    Notification notificationCompat = builder.build();
    NotificationManagerCompat managerCompat =  NotificationManagerCompat.from(this);
    managerCompat.notify(NOTIFICATION_ID, notificationCompat);
}

} }

Manifest 表现

<receiver
android:name=".MyReceiver"
android:enabled="true"
    android:exported="false" >
</receiver>
<service
    android:name=".MyNewIntentService"
    android:exported="false" >
</service>

Java class Java类

    private void Intend()
{
    Context context = this.getApplicationContext();
    Intent notifyIntent = new Intent(this,MyReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast
            (context, 1, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,  System.currentTimeMillis(),
            10000, pendingIntent);
}

Sorry for the bad formatting, I hope it is readable 对不起,格式不好,我希望它可读

No notification is shown :-( Maybe someone can help me out. Thanks to all! 没有显示通知:-(也许有人可以帮助我。谢谢大家!

Try AlarmManager, it is the best option that I can suggest to you according to your requirements. 尝试使用AlarmManager,这是我可以根据您的要求向您建议的最佳选择。 Check this code: 检查此代码:

Calendar cur_cal = new GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis());//set the current time and date for this calendar

 Calendar cal = new GregorianCalendar();
 cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
 cal.set(Calendar.HOUR_OF_DAY, 18);
 cal.set(Calendar.MINUTE, 00);
 cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND));
 cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
 cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
 cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
 Intent intent = new Intent(ProfileList.this, 
 IntentBroadcastedReceiver.class);
 PendingIntent pintent = PendingIntent.getService(ProfileList.this, 0, intent, 0);
 AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
 alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30*1000, pintent);

This code will register the event everyday for 6PM. 该代码将每天下午6点注册该事件。 Now you have to catch the event by adding a broadcast receiver. 现在,您必须通过添加广播接收器来捕获事件。 Here is the code: 这是代码:

public class MyBroadcastReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {

// Generate a push notification here.

}
}

Don't forget to define the Broadcast Receiver in Manifest: 不要忘记在清单中定义广播接收器:

<receiver android:name=".MyBroadcastReceiver" >

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM