Android - 构建一个每周弹出两次的推送通知

时间:2016-12-03 13:45:36

标签: android android-notifications

我想构建一个推送通知,提示设备2次 一周,并将其指向某项活动。你能告诉我如何以及我应该用什么时间来实现这个功能或算法?感谢

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class probe {

    public static String[] extractWords(String text) {

        String pattern = "//[aeiou]\\w{4}\\s";
        Pattern p = Pattern.compile(pattern, Pattern.UNICODE_CASE);
        Matcher m = p.matcher(text);

        int i = 0;

        String[] F = new String[i];
        i = F.length;

        do {
            i++;

        } while (m.find()); //Matches=Länge array

        while (m.find()) {
            String trefferText = m.group();
            F = new String[]{trefferText};
        }

        return F;
    }


    public static void main(String[] args) {

        String text = "";
        String[] Ausgabe = probe.extractWords(text);

        for (String s : Ausgabe) {
            System.out.print(s + " ");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

以下是设置通知通知时间的代码...您也会在应用关闭时收到通知。

public class AlarmTiming {
static AlarmManager alarmManager;
static PendingIntent pendingIntent;
static DatabaseAdapter databaseAdapter;

static String[] pieces;
/*..duration time for alarm to notify you every 3 days ...
mean twice in a week.. you just have to set it again when notification is reached/alarmed
for the next duration*/
static int alarmDuration = 72;
public static void SetTwiceNotification(Context c) {
    String formattedDate = new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime());
    pieces = formattedDate.split(":");
    Calendar calNow = Calendar.getInstance();
    Calendar calSet = (Calendar)calNow.clone();
    calSet.set(Calendar.HOUR_OF_DAY, Integer.parseInt(pieces[0])+alarmDuration);
    calSet.set(Calendar.MINUTE, Integer.parseInt(pieces[1]));
    calSet.set(Calendar.SECOND, 0);
    calSet.set(Calendar.MILLISECOND, 0);
    if(calSet.compareTo(calNow) <= 0) {
        calSet.add(Calendar.DATE, 1);
    }
    setAlarm(calSet,c);
}
private static void setAlarm(Calendar targetCal,Context c ) {

    Intent intent = new Intent(c, AlarmBroadcaster.class);
    //..
    pendingIntent =  PendingIntent.getBroadcast(c, 1, intent,PendingIntent.FLAG_ONE_SHOT);

    alarmManager = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
}

这是接收通知的广播接收器类......

public class AlarmBroadcaster extends BroadcastReceiver {
Context context;
String typeFlag;
int flagId;
@Override
public void onReceive(Context context, Intent intent) {
    this.context = context;
    createNotification(context);
    AlarmTiming.SetTwiceNotification(context);
}

public void createNotification(Context context) {
    PendingIntent pendingIntent = PendingIntent.getActivity(context,0, new Intent(context,LoginActivity.class),0);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.notifi_righter_logo);

    //...
    NotificationCompat.InboxStyle inboxStyle =
            new NotificationCompat.InboxStyle();
    String[] events = {"You are reminded to review your APP"};
    // Sets a title for the Inbox in expanded layout
    inboxStyle.setBigContentTitle(" Notification !");

    // Moves events into the expanded layout
    for (int i=0; i < events.length; i++) {

        inboxStyle.addLine(events[i]);
    }
    // Moves the expanded layout object into the notification object.
    builder.setStyle(inboxStyle);

    builder.setContentIntent(pendingIntent);
    builder.setDefaults(NotificationCompat.DEFAULT_SOUND);
    builder.setAutoCancel(true);

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(1,builder.build());
}

}