android – GCM如何检测app是否打开,如果是这样,弹出一个警告框,而不是正常的通知流程?

前端之家收集整理的这篇文章主要介绍了android – GCM如何检测app是否打开,如果是这样,弹出一个警告框,而不是正常的通知流程?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个应用程序,我想在其中构建2个不同的流程:

> 1.a应用程序对任何活动开放
> 1.b App show是一个警告框,用户可以选择转到有关通知的活动或保持当前活动.
> 2.a应用程序正在后台运行
> 2.b通知栏中的通知,启动有关通知的活动.

我目前有流量2工作,但无法找到如何让流量1工作.这是一些代码

在GcmIntentService中:

@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)) {
                Log.e("GCM","Send error: " + extras.toString());
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
                Log.e("GCM","Deleted messages on server: " + extras.toString());
            // If it's a regular GCM message,do some work.
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                Log.i(TAG,"Completed work @ " + SystemClock.elapsedRealtime());
                // Post notification of received message.
                sendNotification(extras);
                Log.i(TAG,"Received: " + extras.toString());
            }
        }
        // Release the wake lock provided by the WakefulBroadcastReceiver.
        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }

    // Put the message into a notification and post it.
    // This is just one simple example of what you might choose to do with
    // a GCM message.
    private void sendNotification(Bundle extras) {
        mNotificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);

        String message = extras.getString("message");
        Intent openIntent = new Intent(this,HomeActivity.class);
        if (extras != null) {
            if (extras.containsKey("tipid")) {
                openIntent.putExtra("tipid",extras.getString("tipid"));
            } else if (extras.containsKey("discussionid")) {
                openIntent.putExtra("discussionid",extras.getString("discussionid"));
            }
        }

        PendingIntent contentIntent = PendingIntent.getActivity(this,openIntent,PendingIntent.FLAG_CANCEL_CURRENT);
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("StadseBoeren")
        .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(message))
        .setContentText(message);

        mBuilder.setContentIntent(contentIntent)
        .setAutoCancel(true);
        mNotificationManager.notify(NOTIFICATION_ID,mBuilder.build());
    }

GcmBroadcastReceiver

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(Context context,Intent intent) {
        // Explicitly specify that GcmIntentService will handle the intent.
        ComponentName comp = new ComponentName(context.getPackageName(),GcmIntentService.class.getName());
        // Start the service,keeping the device awake while it is launching.
        startWakefulService(context,(intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
    }
}

HomeActivity onCreate:

Bundle extras = intent.getExtras();
if (extras != null) {
    if (extras.containsKey("tipid")) {
        pendingObjectId = extras.getString("tipid");
        modelFlag = ModelFlag.TIP;
    } else if (extras.containsKey("discussionid")) {
        pendingObjectId = extras.getString("discussionid");
        modelFlag = ModelFlag.DISCUSSION;
    }
}

解决方法

使类扩展Application并实现ActivityLifecycleCallbacks
并根据暂停和onResume更新公共布尔值.

在收到push时检查此布尔值并执行您的要求.

希望对你有帮助

public class TestApplication extends Application implements ActivityLifecycleCallbacks{
boolean applicationOnPause = false;
@Override
public void onCreate() {
    super.onCreate();
    registerActivityLifecycleCallbacks(this);
}



@Override
public void onActivityCreated(Activity arg0,Bundle arg1) {
    Log.e("","onActivityCreated");

}
@Override
public void onActivityDestroyed(Activity activity) {
    Log.e("","onActivityDestroyed ");

}
@Override
public void onActivityPaused(Activity activity) {
    applicationOnPause = true;
    Log.e("","onActivityPaused "+activity.getClass());

}
@Override
public void onActivityResumed(Activity activity) {
    applicationOnPause = false;
    Log.e("","onActivityResumed "+activity.getClass());

}
@Override
public void onActivitySaveInstanceState(Activity activity,Bundle outState) {
    Log.e("","onActivitySaveInstanceState");

}
@Override
public void onActivityStarted(Activity activity) {
    Log.e("","onActivityStarted");

}
@Override
public void onActivityStopped(Activity activity) {
    Log.e("","onActivityStopped");

}

}

原文链接:https://www.f2er.com/android/316234.html

猜你在找的Android相关文章