在Android中发送本地通知

9

我想在我的安卓应用中使用本地通知。 如果该应用未打开超过24小时,则会发送本地通知。 有没有人可以告诉我应该如何实现。


我认为你应该创建服务并检查时间,但是要显示通知,你必须先了解相关知识。=) - Gorets
Gorets,你说得很对,我确实需要使用某种服务,因为当应用程序关闭时通知将被触发。你能给我提供一些相关的教程吗? - Mehroze Yaqoob
3个回答

6

参见:Android本地通知? 您可以使用闹钟管理器(schedule an Intent with alarm manager)每小时调度一次。


1
谢谢您的快速回复,但如果应用程序关闭了,能否使用闹钟管理器?如果应用程序关闭了,通知将如何被触发? - Mehroze Yaqoob
是的,即使应用程序关闭,闹钟管理器仍然可以使用。但是,当应用程序安装时,您将无法设置闹钟管理器,只有在应用程序加载(至少一次)时才能设置(请参见:http://stackoverflow.com/a/8492846/986105)。请查看以下内容以使用闹钟管理器创建通知:http://smartandroidians.blogspot.com/2010/04/alarmmanager-and-notification-in.html - KrispyDonuts

3
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());

1
如果你想触发本地通知并带有大量数据,即在单个通知中使用多行文本、标题、滚动条、图标和声音,请使用以下代码。我认为这会对你有所帮助。
        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());

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接