【问题标题】:Sending Local Notifications in Android在 Android 中发送本地通知
【发布时间】:2012-07-11 13:21:44
【问题描述】:

我想在我的应用程序中使用 android 中的本地通知。 如果应用程序在 24 小时内未打开,则会发送本地通知。 谁能告诉我应该怎么做。

【问题讨论】:

  • 我认为您应该创建服务,然后检查时间,但要显示通知,您必须阅读它。 =)
  • Gorets,你说得对,我必须使用某种服务,因为当应用程序关闭时会触发通知。你能给我一些教程吗

标签: android notifications


【解决方案1】:

见:Local Notifications in Android? 您应该可以每小时使用警报管理器安排一次 Intent。

【讨论】:

  • 感谢您的快速响应,但如果应用程序关闭,可以使用警报管理器。应用关闭后如何触发通知?
  • 是的,即使应用程序关闭,警报管理器仍然可以使用。但是,您将无法在安装应用程序时设置警报管理器,只有在加载应用程序时(至少一次)(参见:stackoverflow.com/a/8492846/986105)。看看这个使用警报管理器创建通知:smartandroidians.blogspot.com/2010/04/…
【解决方案2】:
Intent intent = new Intent(context, yourActivity.class);
    PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder b = new NotificationCompat.Builder(context);

    b.setAutoCancel(true)
     .setDefaults(Notification.DEFAULT_ALL)
     .setWhen(System.currentTimeMillis())         
     .setSmallIcon(R.drawable.ic_launcher)
     .setTicker("notification")            
     .setContentTitle("notification")
     .setContentText("notification")
     .setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND)
     .setContentIntent(pIntent)
     .setContentInfo("Info");


    NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, b.build());

【讨论】:

    【解决方案3】:

    如果您想使用大数据触发本地通知,即在单个通知中使用多行文本,带有标题、代码、图标、声音..使用以下代码..我认为它会对您有所帮助..

            Intent notificationIntent = new Intent(context,
                    ReminderListActivity.class);
    
    
    
            notificationIntent.putExtra("clicked", "Notification Clicked");
            notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                    | Intent.FLAG_ACTIVITY_SINGLE_TOP); // To open only one activity
    
    
                // Invoking the default notification service 
    
                NotificationManager mNotificationManager;
                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                        context);
                Uri uri = RingtoneManager
                        .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                mBuilder.setContentTitle("Reminder");
                mBuilder.setContentText("You have new Reminders.");
                mBuilder.setTicker("New Reminder Alert!");
                mBuilder.setSmallIcon(R.drawable.clock);
                mBuilder.setSound(uri);
                mBuilder.setAutoCancel(true);
    
                // Add Big View Specific Configuration 
                NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
                String[] events = null;
    
                    events[0] = new String("Your first line text ");
                    events[1] = new String(" Your second line text");
    
    
    
                // Sets a title for the Inbox style big view
                inboxStyle.setBigContentTitle("You have Reminders:");
    
                // Moves events into the big view
                for (int i = 0; i < events.length; i++) {
                    inboxStyle.addLine(events[i]);
                }
    
                mBuilder.setStyle(inboxStyle);
    
                // Creates an explicit intent for an Activity in your app 
                Intent resultIntent = new Intent(context,
                        ReminderListActivity.class);
    
                TaskStackBuilder stackBuilder = TaskStackBuilder
                        .create(context);
                stackBuilder.addParentStack(ReminderListActivity.class);
    
    
                // Adds the Intent that starts the Activity to the top of the stack
    
    
                stackBuilder.addNextIntent(resultIntent);
                PendingIntent resultPendingIntent = stackBuilder
                        .getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT);
    
                mBuilder.setContentIntent(resultPendingIntent);
                mNotificationManager = (NotificationManager) context
                        .getSystemService(Context.NOTIFICATION_SERVICE);
    
    
                // notificationID allows you to update the notification later  on.
    
    
                mNotificationManager.notify(999, mBuilder.build());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多