Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android GCM消息启动意图_Android_Google Cloud Messaging - Fatal编程技术网

Android GCM消息启动意图

Android GCM消息启动意图,android,google-cloud-messaging,Android,Google Cloud Messaging,现在,我正在根据以下内容创建通知意图: NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); Intent notificationIntent = new Intent(context, MyActivity.class); notificationIntent.setFlags(Intent.FLAG_ACT

现在,我正在根据以下内容创建通知意图:

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
时,额外的内容就丢失了

这有什么关系吗

我一直在尝试将价值储存在其他地方,但没有用。。。这包括共享首选项。

签出。我不知道为什么你的活动应该运行以获得upadate。如果您遵循该教程,那么手机将自动获得推送通知(如果有)。无需在后台运行活动

@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中运行,请仅调用intent以通过该特定活动。如果应用程序正在backgroung中运行,请调用sendnotification()以在通知栏上发送通知。

//此代码放在您的gcmservice中

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

我知道怎么做了

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


您必须通过调用
getIntent()
并检查其是否为null,从
onCreate()
获取意图。案例结束。

kvish您可以检查应用程序是否正在运行?如果应用程序正在运行,则不需要额外的通知意图。对吗?我需要通知意图的原因是因为我有一些数据需要传递给该
活动
。否则,我如何知道用户是否按下了推送通知?我希望能够从推送中提取一些数据并显示相关信息……有没有更简单的方法来做到这一点?并不是因为我无法获得通知。通知中有一些我需要提取的数据。。。这就是我面临的问题。这就是为什么我需要调用
onNewIntent
函数的原因。我的问题更多地围绕这一点:
notificationIntent.putExtra(“key”,value),我需要通过单击推送通知打开应用程序时的值。当应用程序已经打开(或在后台)时,它可以工作,但当应用程序未运行时,该值将丢失。。。我在通知栏中看到了通知,只是没有看到来自
附加功能的值。您可以使用广播接收器,然后您的值就不会丢失不确定我是否收到了此通知…正在阅读
发送广播
,但这是为了我上面提到的通知目的吗?只有在点击通知时才可以发送广播吗?我不清楚。但是当你不处理broadcasr接收器时,你已经在你的应用程序中了。当你的应用程序在你的后台时,通知到达并广播呼叫。