【问题标题】:Local Notifications in Android?Android中的本地通知?
【发布时间】:2011-07-01 20:40:49
【问题描述】:

在 iOS 中,应用程序在后台使用“本地通知”来通知用户发生了一些事情,他们可能需要注意:

本地通知...当您的应用有新数据可用时通知用户,即使您的应用未在前台运行。例如,消息应用程序可能会在新消息到达时通知用户,而日历应用程序可能会通知用户即将到来的约会。

Apple dev - Local and Remote Notifications Overview

[如果应用程序本身提供新数据,则为“本地”; "remote" 如果远程服务器正在发送更新。]

Android 上是否有对应的?

【问题讨论】:

  • 这似乎是这个问题最古老的问题;它与至少一个更高版本的问题相关联,并且该问题的变体至少被问了 4 次。因此,我重写了它以使其不那么模糊,并投票重新打开它。恕我直言,这是一项基本的移动功能;这个问题值得提出和回答。 [或者,如果有更好的答案,可以将其作为后续变体的副本关闭,因为这里不接受任何答案。谷歌“stackoverflow android 本地通知”]
  • 其他线程上的两个有用答案:stackoverflow.com/a/8590724/199364stackoverflow.com/a/31946656/199364 - 从这些我们看到 Android 没有直接等效于 iOS 功能“本地通知”。提到了几个不同的 Android API 可能涉及一个解决方案。因此,我理解共识是否仍然“过于广泛”,尽管我个人希望看到,聚集在一个地方,“后台应用程序,我们应该如何告诉用户发生了重要事情?”的可能替代方案?跨度>

标签: android notifications local


【解决方案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());

【讨论】:

    【解决方案2】:

    如果您也针对旧 API,请使用 NotificationCompat.Builder。

        Intent intent = new Intent(ctx, HomeActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    
        NotificationCompat.Builder b = new NotificationCompat.Builder(ctx);
    
        b.setAutoCancel(true)
         .setDefaults(Notification.DEFAULT_ALL)
         .setWhen(System.currentTimeMillis())         
         .setSmallIcon(R.drawable.ic_launcher)
         .setTicker("Hearty365")            
         .setContentTitle("Default notification")
         .setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
         .setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND)
         .setContentIntent(contentIntent)
         .setContentInfo("Info");
    
    
        NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1, b.build());
    

    【讨论】:

    • 很好的工作示例。我遵循了其他示例代码,它适用于棒棒糖或更高版本。但是这段代码也适用于 kitkat(我不知道为什么......也许是图标或默认值?)谢谢
    【解决方案3】:

    LocalBroadcastManager 看起来是一个更好的解决方案:http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html 创建您自己的自定义 Intent 操作,将其广播到您的进程,并确保将任何活动等注册为该 Intent 的接收器。

    【讨论】:

    • 我的理解是:一个iPhone本地通知,是从你的应用到用户的一种通信。 Android 上的 LocalBroadcastManager 是从应用程序的一个部分到应用程序的另一部分的通信。我认为 LocalBroadcastManager 与该问题无关,但如果我错了,请纠正我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多