android - 在android应用程序中,调用onResume方法时清除状态栏中的小图标

标签 android push-notification google-cloud-messaging android-notifications

我正在开发带有推送通知的 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;
}
}

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

最佳答案

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

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

关于android - 在android应用程序中,调用onResume方法时清除状态栏中的小图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19764388/

相关文章:

android - 如何告诉 RecyclerView 从特定的项目位置开始

react-native - 使用推送通知的 Expo React-Native 裸工作流程

windows-phone-7 - 我可以将推送通知中的参数发送到 Toast (Windows Phone) 吗?

android - 区分 GCM 和 C2DM id

android - Google 旧 GCM 与 Google 新 GoogleCloudMessaging

android - 批量更新会触发多少次 onUpdate 函数?

android - 在android中访问SD卡上的文件

android - 如果 Android 应用程序不在中国 Android 手机的堆栈中,推送通知将不起作用

android - 这个表达式是什么意思 : "R.string.gcm_registered"?

java - "Swig to java (Android)": giving No such file or directory Error on generating Swig to Java file