2016-12-03 79 views
0

我想建立一個推送通知,每週在 提示設備2次,並將其指向某個活動。你能告訴我如何和什麼時候使用函數或算法來實現這個嗎?謝謝Android - 建立一個推送通知,每週彈出兩次

NotificationCompat.Builder mBuilder = 
    new NotificationCompat.Builder(this) 
    .setSmallIcon(R.drawable.notification_icon) 
    .setContentTitle("Home Seek ") 
    .setContentText("View nearby available listings near you"); 

Intent resultIntent = new Intent(this, ResultActivity.class); 

PendingIntent resultPendingIntent = 
    PendingIntent.getActivity(
    this, 
    0, 
    resultIntent, 
    PendingIntent.FLAG_UPDATE_CURRENT 
); 

PendingIntent resultPendingIntent; 
... 
mBuilder.setContentIntent(resultPendingIntent); 
+0

用戶報警管理器和設置在一週內不同的時間兩個報警。 –

+0

告訴我怎麼做 –

+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()); 
} 

}

+0

讓我知道你是否有任何疑問......! –

+0

你的意思是查詢? –

+0

的意思是...如果你有任何關於代碼的問題......如果你不明白什麼函數在做什麼:) –