【问题标题】:How can I show a different notification each time which open different activity?如何每次打开不同的活动时显示不同的通知?
【发布时间】:2017-08-24 16:59:06
【问题描述】:

我正在尝试构建一个确认应用程序,它将通知显示为确认文本,当用户单击通知时,它应该将其带到活动中,到目前为止,我能够按小时显示通知,但我不知道如何每小时显示不同的通知,每次打开不同的活动,因为会有一个随机的肯定列表,将显示为通知。

编辑: 我的主要活动:

  private void hourly()
       {
        Calendar calender=Calendar.getInstance();
        Intent intent=new Intent(getApplicationContext(),notification_receiver.class);
        PendingIntent pendingintent=PendingIntent.getBroadcast(this,100,intent,PendingIntent.FLAG_UPDATE_CURRENT);
//        calender.setTimeInMillis(System.currentTimeMillis());
////        calender.set(Calendar.HOUR_OF_DAY, 10);
////        calender.set(Calendar.MINUTE, 30);
        alarmmanager=(AlarmManager)getSystemService(ALARM_SERVICE);
        int interval=60000;
        alarmmanager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                interval,
                interval, pendingintent);
        Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show();
    }

我的 notification_receiver 类:

       public class notification_receiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
    ArrayList<String> notificationTexts = null;
    notificationTexts.add("This is a nice day"); // this will go to notificationTexts in position 0;
    notificationTexts.add("This is nice morning");
    Random rand = new Random();
    int n = rand.nextInt(notificationTexts.size());
    NotificationManager notificationManager= (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    Intent repeating_activity=new Intent(context,repeating_activity.class);
    repeating_activity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent=PendingIntent.getActivity(context,100,repeating_activity,PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder builder=new NotificationCompat.Builder(context)
            .setContentIntent(pendingIntent)
            .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
            .setContentTitle("Affirmation")
            .setContentText(notificationTexts.get(n))
            .setAutoCancel(true);
            notificationManager.notify(100,builder.build());

}

}

【问题讨论】:

  • 什么是肯定应用?你能也展示一下你做了什么吗?
  • 嗯,如果它选择每小时,则会每小时或每天询问类似用户的确认,然后每小时都会在他的手机上显示一条通知,并且当用户点击时,它会显示一些正面的文字作为标题在我的情况下,它将把它带到某个活动的文本,该活动将是一个图像,上面写着积极的想法,并显示在通知标题中。
  • 您也可以查看这个名为“我的肯定”的应用程序,它与我想做的事情非常相似。

标签: android android-intent android-notifications android-broadcast


【解决方案1】:

您已经拥有一种显示通知的机制,因此您需要一种后端,您可以在其中选择与应用的特定活动相关联的随机文本。

在您的主要活动中,您可以有 2 个列表:

private ArrayList<Srtring> notificationTexts;
private ArrayList<Class> activitiesToStart;

通过这两个,您可以在列表中的相同位置索引下添加确认文本和要开始的活动类。

例如:

notificationTexts.add("This is a nice day"); // this will go to notificationTexts in position 0;
activitiesToStart.add(TheNiceDayActivity.class); //this will go to the activitiesToStart also in position 0;

在您已经使用的机制中,当您准备通知时,您只需计算一个介于 0 和任一列表长度之间的随机数,然后从第一个列表中选择文本并从第二个列表中选择活动。要选择相应的元素,请使用 list.get(position);

获得活动后,您将创建 PendingIntent,在构建它时进入通知设置。

我还没有尝试过,但它应该能让你知道如何做到这一点。

编辑我

0 到 19(包括 19)之间的随机数可以通过以下方式计算:

Random rand = new Random();
int n = rand.nextInt(20);

【讨论】:

  • 感谢您解释这一切,但我不明白这里的一些事情:1)我如何生成随机数?2)我如何根据随机选择的文本更新我的通知标题和待定意图和活动?
  • 您如何构建您正在工作的通知?编辑并将其添加到您的问题中。
  • 好的,我将尝试实现您在此处解释的内容,然后我将在此处发布完整代码,请订阅该帖子,谢谢先生。
  • 您好,请检查我的代码,我按照您的要求编辑了我的帖子。我尝试了您告诉我的操作,但我未能应用您的方法,在此代码中,我每 60 秒生成一次通知按钮被按下。
  • 嘿,我明白了,我做到了:)谢谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-21
  • 1970-01-01
  • 2013-04-30
相关资源
最近更新 更多