更新通知文本

时间:2017-03-25 13:47:51

标签: android sql android-intent notifications alarm

我目前设置了一个小应用程序,它在设备上设置警报并将相关信息存储在SQL数据库中,例如报警名称和时间。警报激活后,我的应用程序会向用户发送通知。目前,每个通知都是静态的,这意味着每条消息都是相同的我现在想让我的应用程序获取已激活的警报的名称并在通知中显示它。现在我知道,在设置多个警报时,您需要多个ID,我已将其存储在SQL的一侧,并且在发送通知时我的猜测相同。有没有一种方法可以将我的Alarms ID与通知中的一个进行匹配,以便它知道要发送的消息,例如报警名称?

设置闹钟的当前代码

pendingIntent = PendingIntent.getBroadcast(view.getContext(), Integer.valueOf(alarm_request_code), receiver, PendingIntent.FLAG_UPDATE_CURRENT);

alarmManager.set(AlarmManager.RTC_WAKEUP, myCalendar.getTimeInMillis(), pendingIntent);

intentArrayList.add(pendingIntent);

我的广播接收器......

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


    Intent moveActivity = new Intent(context, AlarmActivity.class);


    moveActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, moveActivity, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setContentIntent(pendingIntent)
            .setContentTitle("Standard text")
            .setContentText("Standard text")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setAutoCancel(true);


    notificationManager.notify(0, builder.build());



}

//////////////

我的情况更新正在慢慢让我疯狂

我现在可以将文本从我的SQL设置为我的通知,但每次都是我的数据库中的第一个。现在我提出了两种可能的解决方案,这些解决方案只是理论上的。

  1. 是否有一种方法可以在每次调用数据库时将其移至下一个结果?

  2. 正如我上面提到的,我设置了闹钟,当闹钟响起时,它会调用通知。现在,在我对警报的未决意图中,我给它一个id,你可以看到它是alarm_request_code有一种方法可以将它提供给我的通知ID,然后设置一个声明,如果通知ID等于或等于我的警报ID列表存储在我的SQL上,然后它将搜索要输入的正确文本。

    MyDatabaseHandler myDatabaseHandler = new MyDatabaseHandler(context,null,null,1);

    Cursor cursor = myDatabaseHandler.getAllProducts();
    
    
    // Information we are trying to acquire but can only get first result
    String name = cursor.getString(cursor.getColumnIndexOrThrow("alarm_name"));
    String date = cursor.getString(cursor.getColumnIndexOrThrow("alarm_date"));
    
    // Alarm ID FROM SQL which we want to match with the NOTIFICATION ID.....
    String alarm_request_code = cursor.getString(cursor.getColumnIndexOrThrow("alarm_code"));
    
    
    
    ////
    
    
    
    
    Log.i("The reciever is working", "perfect");
    
    //create an intent to service
    
    Intent service_Intent = new Intent(context, MessageService.class);
    context.startService(service_Intent);
    
    //Notification
    
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
    
    
    
    Intent moveActivity = new Intent(context, AlarmActivity.class);
    
    
    moveActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    
    // Works with moveActivity to move the user back to main application.
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, moveActivity, PendingIntent.FLAG_UPDATE_CURRENT);
    
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setContentIntent(pendingIntent)
            .setContentTitle(name)
            .setContentText(date)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setAutoCancel(true);
    
    
    
    
    notificationManager.notify(Integer.valueOf(alarm_request_code), builder.build());
    
    
    
    }
    }
    

2 个答案:

答案 0 :(得分:0)

更新文本存储mBuilder变量 并以这种方式稍后更新:

mBuilder.setContentTitle(head); mBuilder.setContentText(body); mNotificationManager.notify(SIMPLE_ID, mBuilder.build());

修改

    private void simple() {

        Intent intent = new Intent(context, AlarmActivity.class);

        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
        mBuilder = new NotificationCompat.Builder(this);
        mBuilder.setContentTitle(getResources().getString(R.string.app_name))
                .setContentText(getResources().getString(R.string.app_name))
                .setContentIntent(pIntent);

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            mBuilder.setSmallIcon(R.drawable.ic_done_24dp);
        } else {
            mBuilder.setSmallIcon(someiconLollipop);
            mBuilder.setColor(somecolorLollipop]);
        }

        noti = mBuilder.build();
//use flags if you need:
        noti.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
mNotificationManager.notify(SIMPLE_ID, mBuilder.build());
    }

更新文字:

      private void showText() {
            String head = yournewhead;
String body = yournewbody;

            mBuilder.setContentTitle(head);
            mBuilder.setContentText(body);

            mNotificationManager.notify(SIMPLE_ID, mBuilder.build());
        }

答案 1 :(得分:0)

根据文档,您可以执行以下操作:

mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Sets an ID for the notification, so it can be updated
int notifyID = 1;
mNotifyBuilder = new NotificationCompat.Builder(this)
    .setContentTitle("New Message")
    .setContentText("You've received new messages.")
    .setSmallIcon(R.drawable.ic_notify_status)
numMessages = 0;
// Start of a loop that processes data and then notifies the user

    mNotifyBuilder.setContentText(currentText)
        .setNumber(++numMessages);
    // Because the ID remains unchanged, the existing notification is
    // updated.
    mNotificationManager.notify(
            notifyID,
            mNotifyBuilder.build());

这里的诀窍是你的通知id,在你的情况下,为了表现得恰当,它必须是相同的。

因此,只要它没有被解雇,它应该相应地工作。我建议您使用唯一ID作为自定义通知的常量,例如123。

 notificationManager.notify(0, builder.build());