Android中的本地通知? [关闭]

问题描述 投票:45回答:3

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

本地通知...用于在您的应用有新数据时通知用户,即使您的应用未在前台运行也是如此。例如,消息传递应用可以让用户知道新消息何时到达,并且日历应用可以通知用户即将到来的约会。

Apple dev - Local and Remote Notifications Overview

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

在Android上有同等的地方吗?

android notifications local
3个回答
39
投票

如果您也定位旧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());

5
投票

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


4
投票

如果你想用大数据发出本地通知,即在带有标题,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());
© www.soinside.com 2019 - 2024. All rights reserved.