本文介绍了GCM如何检测应用程序是否已打开,如果是,则弹出警告框,而不是正常的通知流程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




  • 1.a应用程序在任何活动中打开

    li>
  • 1.b应用程序秀是一个警报箱,用户可以选择去关于通知的活动或留在当前活动中。
  • 2.a App在后台运行
  • 2.b通知栏中的通知,启动与通知有关的活动。



我目前有2个工作流程,但无法找到如何让工作流程1。以下是一些代码:



在GcmIntentService中:

  @Override 
protected void onHandleIntent(Intent intent){
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// getMessageType()意图参数必须是您在BroadcastReceiver中收到
//的意图。
字符串messageType = gcm.getMessageType(intent);

if(!extras.isEmpty()){//具有不合格的效果Bundle
/ *
*根据消息类型过滤消息。由于GCM很可能在未来使用新的消息类型进行
*扩展,因此忽略任何您感兴趣的消息类型或您不认识的消息类型。
* /
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());
//如果是常规的GCM消息,请做一些工作。
} else if(GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)){
Log.i(TAG,Completed work @+ SystemClock.elapsedRealtime());
//发送收到的消息通知。
sendNotification(extras);
Log.i(TAG,Received:+ extras.toString());
}
}
//释放由WakefulBroadcastReceiver提供的唤醒锁。
GcmBroadcastReceiver.completeWakefulIntent(intent);
}

//将消息放入通知中并发布。
//这只是您可能选择使用
// GCM消息做的一个简单示例。
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,0,
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



<$ p
public void onReceive(Context context,Intent intent){
//显式指定GcmIntentService(); $ p $ public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {

@Override
public void onReceive将处理意图。
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
//启动服务,保持设备在启动时保持清醒状态。
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(discussid);
modelFlag = ModelFlag.DISCUSSION;


$ / code $ / pre

解决方案

Make一个类扩展了Application并且实现了ActivityLifecycleCallbacks
,并根据暂停和onResume更新了一个公共布尔值。



当推送收到时检查此布尔值并按照您的要求执行。



希望这能帮助您

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



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

$ b @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());

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

$ b @Override
public void onActivityStarted(Activity activity){
Log.e(,onActivityStarted);

$ b $ @Override
public void onActivityStopped(Activity activity){
Log.e(,onActivityStopped);

}

}


I have an app where I want to build 2 different flow's in:

  • 1.a App is open on any activity
  • 1.b App show's an alertbox where user can choose to go to the activity regarding the notification or stay on the current activity.

  • 2.a App is running in background

  • 2.b Notification in notification bar, starts the activity regarding to the notification.

I currently have flow 2 working, but cannot find out how to get flow 1 working. Here's some code:

In 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, 0,
                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);
    }
}

The 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;
    }
}
解决方案

Make a class extends Application and implement ActivityLifecycleCallbacksand according to on pause and onResume update a public boolean.

At the time push received check this Boolean and perform as your requirement.

Hope this will help you

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");

}

}

这篇关于GCM如何检测应用程序是否已打开,如果是,则弹出警告框,而不是正常的通知流程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 09:55