在Android中发送本地通知

时间:2012-07-11 13:21:45

标签: android notifications

我想在android中为我的应用程序使用本地通知。 如果应用程序未打开24小时,则发送本地通知。 任何人都可以告诉我应该怎么做。

3 个答案:

答案 0 :(得分:5)

请参阅:Local Notifications in Android? 您应该能够每小时为警报管理员安排一次Intent。

答案 1 :(得分: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());

答案 2 :(得分:0)

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

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