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 に答える