1

我正在开发带有推送通知的 android 应用程序。

当调用 onResume 方法时,我想清除推送通知出现的状态栏中的小图标。

然后,我编写了以下代码,但是它不能很好地工作。

※※PushHandlerActivity.java

@Override
public void onResume() {
    super.onResume();

    GCMIntentService.cancelNotification(this);
    finish();
}

※※GCMIntentService.java

public class GCMIntentService extends GCMBaseIntentService {

public static final int NOTIFICATION_ID = 1234;
private static final String TAG = "GCMIntentService";

public GCMIntentService() {
    super("GCMIntentService");
}

public void createNotification(Context context, Bundle extras)
{
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    String appName = getAppName(this);

    Intent notificationIntent = new Intent(this, PushHandlerActivity.class);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    notificationIntent.putExtra("pushBundle", extras);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(context)
            .setDefaults(Notification.DEFAULT_ALL)
            .setSmallIcon(context.getApplicationInfo().icon)
            .setWhen(System.currentTimeMillis())
            .setContentTitle(extras.getString("title"))
            .setTicker(extras.getString("title"))
            .setContentIntent(contentIntent);

    String message = extras.getString("message");
    if (message != null) {
        mBuilder.setContentText(message);
    } else {
        mBuilder.setContentText("<missing message content>");
    }

    String msgcnt = extras.getString("msgcnt");
    if (msgcnt != null) {
        mBuilder.setNumber(Integer.parseInt(msgcnt));
    }

    mNotificationManager.notify((String) appName, NOTIFICATION_ID, mBuilder.build());
    mBuilder.setAutoCancel(true);
}

public static void cancelNotification(Context context)
{
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.cancel((String)getAppName(context), NOTIFICATION_ID);  
}

private static String getAppName(Context context)
{
    CharSequence appName = 
            context
                .getPackageManager()
                .getApplicationLabel(context.getApplicationInfo());

    return (String)appName;
}
}

如果你能告诉我如何解决这个问题,我将不胜感激。我被困住了!

4

2 回答 2

5

最简单的方法是自动取消通知:

.setAutoCancel(true)

这将在点击通知时自动删除通知。

这是您在代码中添加它的地方:

NotificationCompat.Builder mBuilder =
    new NotificationCompat.Builder(context)
        .setDefaults(Notification.DEFAULT_ALL)
        .setSmallIcon(context.getApplicationInfo().icon)
        .setWhen(System.currentTimeMillis())
        .setContentTitle(extras.getString("title"))
        .setTicker(extras.getString("title"))
        .setContentIntent(contentIntent)
        .setAutoCancel(true);
于 2013-11-04T13:28:48.083 回答
0
private static final int MY_NOTIFICATION_ID= 1234;
String NotiSer = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager;
mNotificationManager = (NotificationManager) getSystemService(NotiSer);
mNotificationManager.notify(MY_NOTIFICATION_ID, notification); 

要取消此通知:

mNotificationManager.cancel(MY_NOTIFICATION_ID);
于 2013-11-04T09:10:39.090 回答