简体   繁体   English

Android上的多个推送通知,图标大小错误

[英]Multiple push notifications on android with wrong icon size

Let me preface this post with the fact that I am new to the android programming. 让我以我是android编程新手的事实作为这篇文章的开头。

When I issue a notification, the phone shows two notifications for the same thing. 当我发出通知时,手机会为同一件事显示两个通知。 The only difference is, the icons look different and they do different things. 唯一的区别是,图标看起来不同并且它们执行不同的操作。 The first will do nothing when you select it. 选择它时,第一个将不执行任何操作。 The other opens the app to the correct page. 另一个将应用程序打开到正确的页面。

I used to only get one notification until I edited this method. 在修改此方法之前,我以前只会收到一个通知。 I'm not sure what I did but perhaps someone could tell me why this sends 2 and how to fix it? 我不确定自己做了什么,但是也许有人可以告诉我为什么发送2并解决该问题?

The other question I have is that on both notifications on the phone, the icon is zoomed in (or too big). 我的另一个问题是,在电话上的两个通知中,图标均被放大(或太大)。 How do you make the push notification icon the correct size? 如何使推送通知图标的大小正确?

Here is my method 这是我的方法

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




}

Try this : 尝试这个 :

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

Hope this helps.This works for me. 希望这会有所帮助,这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM