我尝试在for循环中构建本地通知,但是仅在android

时间:2018-03-07 11:24:19

标签: android

for (int i = 0; i < db.getDataCount(); i++) {

        sendNotification.notificationsend(notificationDataList.get(i), i);

        final AlarmManager alarmManager = (AlarmManager)
 getSystemService(Context.ALARM_SERVICE);

//            final Intent notificationIntent = new Intent(this,
 AlarmReceiver.class);

        Intent notificationIntent = new 
Intent("android.media.action.DISPLAY_NOTIFICATION");
        notificationIntent.addCategory("android.intent.category.DEFAULT");

        notificationIntent.putExtra(Constants.NOTIFICATION_MESSAGE,
 notificationDataList.get
                (i).getMessage());
        notificationIntent.putExtra(Constants.NOTIFICATION_ID, i);
        final PendingIntent broadcast = 
PendingIntent.getBroadcast(getBaseContext(), 100,
                notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        final Calendar cal = Calendar.getInstance();
        cal.setTime(notificationDataList.get(i).getDateAndTime());
        hour = cal.get(Calendar.HOUR_OF_DAY);
        minutes = cal.get(Calendar.MINUTE);
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.MINUTE, minutes);
        cal.set(Calendar.SECOND, 1);


        System.out.println("Hour + " + hour + " Minutes " + minutes);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            alarmManager.setExact(AlarmManager.RTC_WAKEUP,
 cal.getTimeInMillis(), broadcast);
        }

    }

public void onReceive(Context context, Intent intent) {

    System.out.println("Inside Broad cast receiver!!");
    Intent notificationIntent = new Intent(context, 
NotificationActivity.class);

    TaskStackBuilder stackBuilder = null;
//        if (android.os.Build.VERSION.SDK_INT >=  
android.os.Build.VERSION_CODES.JELLY_BEAN) {
    stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(NotificationActivity.class);
    stackBuilder.addNextIntent(notificationIntent);

    String message = 
 intent.getStringExtra(Constants.NOTIFICATION_MESSAGE);
    int notificationId = intent.getIntExtra(Constants.NOTIFICATION_ID, 0);
 //            PendingIntent pendingIntent = 
 //stackBuilder.getPendingIntent(0,
//                    PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,
            PendingIntent.FLAG_UPDATE_CURRENT);
    Notification.Builder builder = new Notification.Builder(context);

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

    Uri uri = 
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    notification = builder.setContentTitle("Wrizto App Notification")
            .setContentText(message)
            .setTicker("New Message Alert!")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setColor(context.getResources().getColor(R.color.white))
            .setAutoCancel(true)
            .setSound(uri)
            .setContentIntent(pendingIntent).build();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        builder.setChannelId(CHANNEL_ID);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(
                CHANNEL_ID,
                "NotificationDemo",
                IMPORTANCE_DEFAULT
        );
        channel.setDescription("Karkloof Fallsat the Karkloof Nature 
Reserve " +
                "in the Province " + "of KwaZulu - Nata, South Africa");
        channel.enableLights(true);
        channel.setLightColor(Color.BLUE);
        channel.setSound(uri, null);
        channel.setShowBadge(true);
        notificationManager.createNotificationChannel(channel);
    }

    notificationManager.notify(notificationId, notification);

}

我从服务器获取数据,然后将其存储在SQLite中。在MyService中,我从SQLite获取数据,这是一个进程,数据在for循环中读取每一行,并尝试使用警报管理器为每一行构建本地Notification以在特定时间获取通知。但是通知仅针对for循环的最后一个循环构建。

1 个答案:

答案 0 :(得分:0)

哦!我犯了一个错误,Pending Intent Request代码必须对于for循环中的每个通知都是唯一的