我的应用程序已经集成了 GCM
推送通知。每当有通知时,它都能正常显示。但是,即使用户在应用程序内部,推送通知也会出现。我希望只有当用户在应用程序外部时才会出现通知。
以下是我的推送通知代码:
GcmBroadcastReceiver
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(GCMNotificationIntentService.class.getPackage().getName(),
GCMNotificationIntentService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
GCMnotificationIntentService.Class
public class GCMNotificationIntentService extends IntentService {
public static int notificationId = 10;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
private boolean login_status = false;
public GCMNotificationIntentService() {
super("GcmIntentService");
}
public static final String TAG = GCMNotificationIntentService.class.getSimpleName();
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
if ( !extras.isEmpty() ) { // has effect of unparcelling Bundle
/*
* Filter messages based on message type. Since it is likely that
* GCM will be extended in the future with new message types, just
* ignore any message types you're not interested in, or that you
* don't recognize.
*/
if ( GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType) ) {
sendNotification("Send error: " + extras.toString());
} else if ( GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType) ) {
sendNotification("Deleted messages on server: " + extras.toString());
// If it's a regular GCM message, do some work.
} else if ( GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType) ) {
sendNotification(extras.getString("message"));
Log.i(TAG, "Received: " + extras.toString());
}
}
// Release the wake lock provided by the WakefulBroadcastReceiver.
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getString(R.string.notification_title))
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg)).setContentText(msg);
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setSound(uri);
mBuilder.setContentIntent(contentIntent);
mBuilder.setAutoCancel(true);
mNotificationManager.notify(notificationId++, mBuilder.build());
}
}
在 ManifestFile 中
<receiver
android:name=".GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.medlife.deliveryagent" />
</intent-filter>
</receiver>
<service android:name=".GCMNotificationIntentService" />
任何建议都将不胜感激!!!