2016-12-03 5 views
0

私は、デバイスを1週間に2回呼び出すプッシュ通知を作成して、特定のアクティビティに誘導したいと考えています。あなたはこれを実装するために関数やアルゴリズムをどのように使うべきか、どのような時に私を見せてもらえますか?ありがとうAndroid - 1週間に2回ポップアップするプッシュ通知を作成する

+0

ユーザーアラームマネージャと週に異なる時間のための2つのアラームを設定し、通知を受信する放送受信機クラスです。 –

+0

私にそれを行う方法を教えてください –

+0

ok私はあなたのためのコードをアセンブルさせてください....私の答えに見てください –

答えて

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

意味...コードに関する質問がある場合...どのような機能が何をしているのか理解できない場合:) –

関連する問題