android gcm推送通知随机打开应用程序

时间:2014-09-11 16:08:07

标签: android notifications push google-cloud-messaging

我们的应用程序中有一个奇怪的错误。我们的应用程序旨在接收GCM推送通知,并根据通知单击时通知的有效负载将应用程序打开到特定活动。这一切都很好,除了在极少数情况下(因为我已经触发了500多个推送通知并且只看到它发生了两次)我们的应用程序将在没有用户干预的情况下打开自己。看起来似乎有什么东西在通知栏中触发了通知。有没有人经历过类似的事情或对可能导致这种情况的原因有任何想法?我已经非常了解谷歌和StackOverflow,但我只发现了一个关于类似行为的问题,没有答案可以在这里看到https://stackoverflow.com/questions/19112479/push-notification-is-opening-app-directly

我已经在下面的清单中包含了BroadcastReceiver,IntentService和intent-filters。

广播接收器:

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        ComponentName comp = new ComponentName(context.getPackageName(), GCMIntentService.class.getName());
        startWakefulService(context, intent.setComponent(comp));
        setResultCode(Activity.RESULT_OK);
    }
}

IntentService:

public class GCMIntentService extends IntentService {
    private static final int NOTIFICATION_ID = 100;

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

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();

        if (!extras.isEmpty()) {            
            GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

            if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(gcm.getMessageType(intent)))
                sendNotification(extras.getString(GlooIntents.GLOO_URL), extras.getString(GlooIntents.ALERT));
        }

        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }

    private void sendNotification(String url, String message) {
        long when = System.currentTimeMillis();

        Intent notificationIntent = new Intent(this, SplashActivity.class);
        notificationIntent.putExtra("nextPath", url);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_NEW_TASK);

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

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

        builder.setSmallIcon(R.drawable.ic_home);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_home));
        builder.setWhen(when);

        builder.setContentIntent(pendingIntent);
        builder.setContentTitle(getString(R.string.app_name_replace_me));
        builder.setContentText(message);
        builder.setAutoCancel(true);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancel(NOTIFICATION_ID);
        notificationManager.notify(NOTIFICATION_ID, builder.getNotification());
    }
}

清单

<activity
    android:name=".GroupActivity"
    android:label="@string/group"
    android:screenOrientation="portrait"
    android:windowSoftInputMode="adjustResize" >
    <intent-filter>
        <data
            android:host="app"
            android:pathPattern="/groups/.*"
            android:scheme="gloo" />

            <action android:name="android.intent.action.VIEW" />

        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
    </intent-filter>
</activity>

0 个答案:

没有答案