0

让我以我是 android 编程新手的事实作为这篇文章的序言。

当我发出通知时,手机会针对同一件事显示两个通知。唯一的区别是,图标看起来不同,它们做的事情也不同。当你选择它时,第一个不会做任何事情。另一个将应用程序打开到正确的页面。

在我编辑此方法之前,我曾经只收到一个通知。我不确定我做了什么,但也许有人可以告诉我为什么会发送 2 以及如何解决它?

我的另一个问题是,在手机上的两个通知中,图标都被放大(或太大)。您如何使推送通知图标的大小正确?

这是我的方法

  private static void generateNotification(Context context, String message) {
    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, FriendGroupActivity.class);

    // 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;

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




}
4

1 回答 1

0

尝试这个 :

private void generateNotification(Context context, String message) {

int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
String appname = context.getResources().getString(R.string.app_name);
NotificationManager notificationManager = (NotificationManager) context
    .getSystemService(Context.NOTIFICATION_SERVICE);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
Notification notification;
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
    new Intent(context, myactivity.class), 0);

// To support 2.3 os, we use "Notification" class and 3.0+ os will use
// "NotificationCompat.Builder" class.
if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
notification = new Notification(icon, message, 0);
notification.setLatestEventInfo(context, appname, message,
        contentIntent);
notification.flags = Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);

} else {
NotificationCompat.Builder builder = new NotificationCompat.Builder(
        context);
notification = builder.setContentIntent(contentIntent)
        .setSmallIcon(icon).setTicker(appname).setWhen(0)
        .setAutoCancel(true).setContentTitle(appname)
        .setContentText(message).build();

notificationManager.notify(0 , notification);
}
}

希望这会有所帮助。这对我有用。

于 2013-11-06T07:43:37.363 回答