Android 收到GCM消息时,如何启动应用程序而不是生成通知?

Android 收到GCM消息时,如何启动应用程序而不是生成通知?,android,google-cloud-messaging,android-notifications,Android,Google Cloud Messaging,Android Notifications,我刚刚实现了一个GCM应用程序,它在GCM消息到达时显示通知。当消息到达时,如何启动应用程序?和毒蛇一样。当消息到达时,您会看到一个弹出框 编辑: 非常感谢您的帮助,但我想你们大多数人都解释过,为了启动应用程序,需要用户点击通知。我需要让活动在GCM消息到达时自动启动,而不管前台是什么应用程序,甚至应用程序处于后台或状态 这是我的GCMinentService代码: package com.google.android.gcm.demo.app; import com.google.andro

我刚刚实现了一个GCM应用程序,它在GCM消息到达时显示通知。当消息到达时,如何启动应用程序?和毒蛇一样。当消息到达时,您会看到一个弹出框

编辑:

非常感谢您的帮助,但我想你们大多数人都解释过,为了启动应用程序,需要用户点击通知。我需要让活动在GCM消息到达时自动启动,而不管前台是什么应用程序,甚至应用程序处于后台或状态

这是我的GCMinentService代码:

package com.google.android.gcm.demo.app;

import com.google.android.gms.gcm.GoogleCloudMessaging;

import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;


public class GCMIntentService extends IntentService {
    public static final int NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;

    public GCMIntentService() {
        super("GcmIntentService");
    }
    public static final String TAG = "GCM Demo";

    @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)) {
                // This loop represents the service doing some work.
                for (int i = 0; i < 5; i++) {
                    Log.i(TAG, "Working... " + (i + 1)
                            + "/5 @ " + SystemClock.elapsedRealtime());
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                    }
                }
                Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                startActivity(new Intent(getBaseContext(), DemoActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
                Intent myIntent = new Intent(getBaseContext(), DemoActivity.class);
                startActivity(myIntent);
                // Post notification of received message.
                //sendNotification("Received: " + extras.toString());
                Log.i(TAG, "Received: " + extras.toString());
                Toast.makeText(getApplicationContext(), extras.toString(), Toast.LENGTH_LONG).show();
            }
        }
        // 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(String msg) {
        mNotificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, DemoActivity.class), 0);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_stat_gcm)
                        .setContentTitle("GCM Notification")
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(msg))
                        .setContentText(msg);
           Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_LONG).show();

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }
}
package com.google.android.gcm.demo.app;
导入com.google.android.gms.gcm.GoogleCloudMessaging;
导入android.app.IntentService;
导入android.app.NotificationManager;
导入android.app.pendingent;
导入android.content.Context;
导入android.content.Intent;
导入android.os.Bundle;
导入android.os.SystemClock;
导入android.support.v4.app.NotificationCompat;
导入android.util.Log;
导入android.widget.Toast;
公共类GCMinentService扩展了IntentService{
公共静态最终整数通知_ID=1;
私人通知经理通知经理;
通知建筑商;
公共GCMinentService(){
超级(“GCMinentService”);
}
公共静态最终字符串TAG=“GCM Demo”;
@凌驾
受保护的手部内容无效(意图){
Bundle extras=intent.getExtras();
GoogleCloudMessaging gcm=GoogleCloudMessaging.getInstance(this);
//getMessageType()意图参数必须是您收到的意图
//在你的收音机里。
字符串messageType=gcm.getMessageType(intent);
如果(!extras.isEmpty()){//具有解包的效果
/*
*根据消息类型筛选消息。因为GCM可能会
*在将来使用新的消息类型进行扩展,只需忽略您正在使用的任何消息类型
*不感兴趣,或者你不认识。
*/
if(GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)){
sendNotification(“发送错误:+extras.toString());
}else if(GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)){
sendNotification(“服务器上的已删除邮件:+extras.toString());
//如果是常规GCM消息,请执行一些操作。
}else if(GoogleCloudMessaging.MESSAGE\u TYPE\u MESSAGE.equals(messageType)){
//此循环表示正在执行某些工作的服务。
对于(int i=0;i<5;i++){
Log.i(标记“正在工作…”+(i+1)
+“/5@”+SystemClock.elapsedRealtime());
试一试{
睡眠(5000);
}捕捉(中断异常e){
}
}
Log.i(标记“Completed work@”+SystemClock.elapsedRealtime());
startActivity(新意图(getBaseContext(),DemoActivity.class).setFlags(意图.FLAG_活动_清除_顶部));
Intent myIntent=newintent(getBaseContext(),DemoActivity.class);
星触觉(myIntent);
//接收到消息的Post通知。
//sendNotification(“已收到:+extras.toString());
Log.i(标记“Received:+extras.toString());
Toast.makeText(getApplicationContext(),extras.toString(),Toast.LENGTH\u LONG.show();
}
}
//释放唤醒接收器提供的唤醒锁。
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
//将消息放入通知并发布。
//这只是一个简单的例子,说明您可能会选择如何处理
//GCM消息。
私有void sendNotification(字符串msg){
mNotificationManager=(NotificationManager)
this.getSystemService(Context.NOTIFICATION\u服务);
PendingEvent contentIntent=PendingEvent.getActivity(此,0,
新意图(此,DemoActivity.class),0);
通知相容建筑商mBuilder=
新建NotificationCompat.Builder(此)
.setSmallIcon(R.drawable.ic_stat_gcm)
.setContentTitle(“GCM通知”)
.setStyle(新通知Compat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_LONG.show();
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID,mBuilder.build());
}
}

您可以使用
pendingent

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, DemoActivity.class), 0);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.ic_stat_gcm)
    .setContentTitle("GCM Notification")
    .setStyle(new NotificationCompat.BigTextStyle()
    .bigText(msg))
    .setContentText(msg);

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


如果您愿意,您可以使您的
演示活动
对话框类似于或其他内容。

通常的方法是通过
挂起内容
将其添加到您的
通知中。例如,生成器
通知

PendingIntent intent = PendingIntent.getActivity(context, 0, new Intent(context, HomeActivity.class), 0);
notificationBuilder.setContentIntent(pendingIntent);
要添加弹出框,您可以看到完整的答案

编辑:


刚刚看到您的评论以便启动活动只需调用
startActivity()gcminentservice中的code>。

您可以直接使用包名/类,例如创建调用twidroid程序的新意图,您可以使用以下文本:


您可能希望在未安装应用程序的情况下为ActivityNotFoundException设置一个try/catch选项。

将其添加到GCMinentService类中

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

                } else if (GoogleCloudMessaging.
                        MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                    ** //Start your activity here .This will automatically launch your activity**
                }
            }

请参阅此链接。。创建对话框并在收到GCM消息时显示它,而不是通知。使用广播接收器而不是挂起的意图谢谢,但我猜您所解释的内容要求用户单击o
@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
                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());

                } else if (GoogleCloudMessaging.
                        MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                    ** //Start your activity here .This will automatically launch your activity**
                }
            }