【问题标题】:Use notification ID to change logo & text使用通知 ID 更改徽标和文本
【发布时间】:2013-05-19 17:19:00
【问题描述】:

我想知道 Facebook 等应用程序如何根据内容更改通知标题和徽标。

例如,在 Facebook 中,如果您被标记,您将获得另一个标题和另一个徽标。

我认为通过 notify 应该可以创建一个唯一的通知。 虽然我找不到任何明确的例子。

我的生成通知:

private static void generateNotification(Context context, String message, String url) {
    int icon = R.drawable.ic_launcher;

    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager)
    context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);

    String title = context.getString(R.string.app_name);

    Intent notificationIntent = new Intent(context, ShowChange.class);
    notificationIntent.putExtra ("url",url);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
            Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent =
            PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    // Play default notification sound
    notification.defaults |= Notification.DEFAULT_SOUND;

    //notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");

    // Vibrate if vibrate is enabled
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);       
}

【问题讨论】:

    标签: java android push-notification google-cloud-messaging


    【解决方案1】:

    应该是这样的:(这基本上是你的代码,经过一些修改)

    public static void generateNotification(Context context, String message, String url,int icon_from_drawable) { int icon = icon_from_drawable;

    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager)
    context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);
    
    String title = context.getString(R.string.app_name);
    
    Intent notificationIntent = new Intent(context, ShowChange.class);
    notificationIntent.putExtra ("url",url);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
            Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent =
            PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    
    // Play default notification sound
    notification.defaults |= Notification.DEFAULT_SOUND;
    
    //notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");
    
    // Vibrate if vibrate is enabled
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);      
    

    }

    如果您将应用程序定位为 API LEVEL>=11,则使用 Notification.Builder 参考这里:http://developer.android.com/reference/android/app/Notification.Builder.html 还有一点,上面的代码会不断更新同一个通知对象,因为通知管理器的 notify 方法总是采用相同的 id 即 0,所以会更新同一个通知对象。

    【讨论】:

    • 好主意,谢谢,我会在 php 上发送带有 GCM 的图标名称作为额外信息,我会在几分钟内尝试:P
    • Thnx,你的想法解决了我的问题。我现在从我的服务器获取图像,我将其作为字符串接收,而不是像这样将其解析为 int: int icon = context.getResources().getIdentifier(icon_from_drawable, "drawable", context.getPackageName());这会将其解析为 int 并从服务器获取 png :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-01
    • 2012-10-30
    • 2011-04-26
    • 2017-02-14
    相关资源
    最近更新 更多