GCM消息启动Intent

时间:2013-12-17 04:04:08

标签: android google-cloud-messaging

现在我按照以下方式创建通知意图:

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, MyActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.setAction(Long.toString(System.currentTimeMillis()));

notificationIntent.putExtra("key", value);

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
Notification updateComplete = new NotificationCompat.Builder(context)
    .setContentTitle(title)
    .setContentText(msg)
    .setTicker(title)
    .setWhen(System.currentTimeMillis())
    .setContentIntent(contentIntent)
    .setDefaults(Notification.DEFAULT_SOUND)
    .setAutoCancel(true)
    .setSmallIcon(R.drawable.icon_notifications)
    .build();

notificationManager.notify(100, updateComplete);

当应用已经在后台时,一切正常。函数onNewIntent被调用,我从notificationIntent extras中获取值。但是,如果应用程序不在后台,则会转到根活动(登录屏幕),该活动会将用户转发到MyActivity。但是根活动不会调用onNewIntent,当用户到达MyActivity时,额外内容就会丢失。

到底有没有?

我一直试图将值存储在其他地方但无济于事......这包括共享首选项。

4 个答案:

答案 0 :(得分:1)

@Override
    protected void onMessage(final Context ctx, Intent intent) {
if (CommonMethod.isAppicationRunning(ctx)) {
            // Creates an explicit intent for an Activity in your app
            Intent resultIntent = new Intent(ctx, ViewMessageDialog.class);
            resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            resultIntent.putExtra("gcmmessage", message);
            ctx.startActivity(resultIntent);
        } else {

            try {
                sendNotification(message);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

    }

/////////////////////////////////////////////// /////////////////////////////

public static boolean isAppicationRunning(Context activity) {
        ActivityManager am = (ActivityManager) activity
                .getSystemService(EGLifeStyleApplication.mContext.ACTIVITY_SERVICE);

        // get the info from the currently running task
        List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
        ComponentName componentInfo = taskInfo.get(0).topActivity;
        Log.i("current activity", "activity"
                + activity.getClass().getSimpleName());

        if (componentInfo.getPackageName().equals(
                EGLifeStyleApplication.mContext.getPackageName())) {
            return true;
        }
        return false;
    }

/////////////////////////////////////////////// /////////////////////////////////////

private void sendNotification(String message) throws JSONException {
        // this
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

        int icon = R.drawable.app_icon;
        CharSequence tickerText = message; // ticker-text
        long when = System.currentTimeMillis();
        Context context = getApplicationContext();
        context.getClass().getSimpleName();
        Log.i("context.getClass().getSimpleName()",
                "context.getClass().getSimpleName()="
                        + context.getClass().getSimpleName());

        CharSequence contentTitle = "Product Received.";
        CharSequence contentText = message;
        Intent notificationIntent = null;
        int notificationID = CommonVariable.notificationID;

        notificationIntent = new Intent(this, ViewHomeScreen.class);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                notificationIntent, 0);
        Notification notification = new Notification(icon, tickerText, when);
        // Play default notification sound
        notification.defaults |= Notification.DEFAULT_ALL;
        notification.setLatestEventInfo(context, contentTitle, contentText,
                contentIntent);
        mNotificationManager.notify(notificationID, notification);
    }

检查应用程序是否正在运行?如果应用程序在forgroung中运行,那么请仅调用意图传递该特定活动。如果应用程序在backgroung中运行,则调用sendnotification()以在通知栏上发送通知。

答案 1 :(得分:1)

//此代码放在您的gcmservice

Intent intent2 = new Intent();
                intent2.setAction("RECEIVE_MESSAGE_ACTION_NEW");
                intent2.putExtra("senderNum", senderNum);
                intent2.putExtra("verificationCode",
                        ReturnValidationcode(message));
                context.sendBroadcast(intent2);

答案 2 :(得分:0)

检查this。我不知道为什么你的活动应该运行以获得更新。如果您遵循该教程,那么移动设备将自动获得推送通知(如果有)。无需在后台运行活动。

答案 3 :(得分:0)

我发现了如何做到这一点。

基本上,如果您使用notificationIntent启动的活动已经打开(如果它是用户所在的当前活动),则会调用onNewIntent(Intent intent)。如果它当前不是活动活动或应用程序未运行,它将启动活动并转到onCreate()

您必须通过调用onCreate()并检查它是否为空来从getIntent()获取意图。案件结案。