仅在应用未运行android时显示推送通知

约翰

在我的应用程序中,我集成了的推送通知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" />

任何建议将不胜感激!!!

潘卡伊

通过调用以下方法并根据返回值true或false来执行以下操作,以检查您的应用程序是否处于前台状态

public boolean checkApp(){
       ActivityManager am = (ActivityManager) this
                .getSystemService(ACTIVITY_SERVICE);

        // get the info from the currently running task
        List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);

        ComponentName componentInfo = taskInfo.get(0).topActivity;
        if (componentInfo.getPackageName().equalsIgnoreCase("YourPackage")) {
            return true;
        } else {
           return false;
        }
  } 

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在应用未打开时显示推送通知

来自分类Dev

应用未运行/应用终止时如何获取推送通知

来自分类Dev

解析SDK iOS-推送通知仅在应用程序处于前台时显示

来自分类Dev

如果应用未运行一周以上未打开,则推送通知(Android)

来自分类Dev

Android推送通知消息未显示

来自分类Dev

Android推送通知消息未显示

来自分类Dev

当应用未打开时,Android显示通知吗?

来自分类Dev

滑动时未删除android推送通知

来自分类Dev

推送通知未显示

来自分类Dev

推送通知未显示

来自分类Dev

解析推送通知仅在应用重启时收到

来自分类Dev

收到推送通知时打开Android应用

来自分类Dev

收到推送通知时打开Android应用

来自分类Dev

推送通知未显示在 Android 通知栏中

来自分类Dev

应用徽章未与解析推送通知一起显示

来自分类Dev

应用徽章未与分析推送通知一起显示

来自分类Dev

当应用未运行时,Android通知未显示其内容

来自分类Dev

Cordova / Phonegap Android解析推送通知未恢复应用

来自分类Dev

在应用关闭时从推送通知中显示ViewController的正确方法

来自分类Dev

如果在应用未运行时收到推送通知,则通知会消失

来自分类Dev

在点击城市飞艇的推送通知时打开android应用吗?

来自分类Dev

iOS; 当应用未运行或在后台运行时,推送通知不起作用

来自分类Dev

当应用未运行时处理推送通知(应用被终止)

来自分类Dev

在应用程序运行时如何在通知栏中显示“解析推送通知”?

来自分类Dev

Firebase推送通知仅在应用程序位于前台ios中时接收

来自分类Dev

当应用程序在后台(钛)运行时,Android 5上的推送通知未传递-但在前台运行

来自分类Dev

GCM推送通知未打开应用

来自分类Dev

GCM推送通知未打开应用

来自分类Dev

图片未显示在推送通知中

Related 相关文章

  1. 1

    在应用未打开时显示推送通知

  2. 2

    应用未运行/应用终止时如何获取推送通知

  3. 3

    解析SDK iOS-推送通知仅在应用程序处于前台时显示

  4. 4

    如果应用未运行一周以上未打开,则推送通知(Android)

  5. 5

    Android推送通知消息未显示

  6. 6

    Android推送通知消息未显示

  7. 7

    当应用未打开时,Android显示通知吗?

  8. 8

    滑动时未删除android推送通知

  9. 9

    推送通知未显示

  10. 10

    推送通知未显示

  11. 11

    解析推送通知仅在应用重启时收到

  12. 12

    收到推送通知时打开Android应用

  13. 13

    收到推送通知时打开Android应用

  14. 14

    推送通知未显示在 Android 通知栏中

  15. 15

    应用徽章未与解析推送通知一起显示

  16. 16

    应用徽章未与分析推送通知一起显示

  17. 17

    当应用未运行时,Android通知未显示其内容

  18. 18

    Cordova / Phonegap Android解析推送通知未恢复应用

  19. 19

    在应用关闭时从推送通知中显示ViewController的正确方法

  20. 20

    如果在应用未运行时收到推送通知,则通知会消失

  21. 21

    在点击城市飞艇的推送通知时打开android应用吗?

  22. 22

    iOS; 当应用未运行或在后台运行时,推送通知不起作用

  23. 23

    当应用未运行时处理推送通知(应用被终止)

  24. 24

    在应用程序运行时如何在通知栏中显示“解析推送通知”?

  25. 25

    Firebase推送通知仅在应用程序位于前台ios中时接收

  26. 26

    当应用程序在后台(钛)运行时,Android 5上的推送通知未传递-但在前台运行

  27. 27

    GCM推送通知未打开应用

  28. 28

    GCM推送通知未打开应用

  29. 29

    图片未显示在推送通知中

热门标签

归档