Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.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 未从通知中启动PendingEvent_Android - Fatal编程技术网

Android 未从通知中启动PendingEvent

Android 未从通知中启动PendingEvent,android,Android,我已经尝试了大约20种不同的答案,但没有一种是有效的 只是想从我的通知中启动一个活动。它不起作用了。它没有做任何事情,如果我进入我的主屏幕并点击它,它甚至不会打开应用程序 我从一开始就添加了很多东西,所以对于所有可能不需要的东西感到抱歉。我只是一直在寻找答案 private void sendNotification(String msg, String cont, String title) { mNotificationManager = (NotificationManager)

我已经尝试了大约20种不同的答案,但没有一种是有效的

只是想从我的通知中启动一个活动。它不起作用了。它没有做任何事情,如果我进入我的主屏幕并点击它,它甚至不会打开应用程序

我从一开始就添加了很多东西,所以对于所有可能不需要的东西感到抱歉。我只是一直在寻找答案

private void sendNotification(String msg, String cont, String title) {
    mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
    //        this.getSystemService(Context.NOTIFICATION_SERVICE);
Log.d("msg",msg);

int requestID = (int) System.currentTimeMillis();
Intent notificationIntent = new Intent(getApplicationContext(), WalkerRequest.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Log.d("Intent","setting content intent");
PendingIntent contentIntent = PendingIntent.getActivity(this, requestID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT
        | PendingIntent.FLAG_ONE_SHOT);

NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.paw_icon)
                .setContentTitle(title)
                .setStyle(new NotificationCompat.BigTextStyle()
                        .bigText(cont))
                .setContentText(cont);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
这是完整的类,它是从谷歌直接几乎

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(), "Nada");
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
                sendNotification("Deleted messages on server: ", extras.toString(), "nada");
                // 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(500);
                    } catch (InterruptedException e) {
                    }
                }
                String cont = intent.getExtras().getString("message");
                String title = intent.getExtras().getString("title");

                Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                // Post notification of received message.
                sendNotification("Received: ", cont, title);
                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(String msg, String cont, String title) {
        mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
        //        this.getSystemService(Context.NOTIFICATION_SERVICE);

        Log.d("msg",msg);

        int requestID = (int) System.currentTimeMillis();
        Intent notificationIntent = new Intent(getApplicationContext(), WalkerRequest.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        Log.d("Intent","setting content intent");
        PendingIntent contentIntent = PendingIntent.getActivity(getBaseContext(), requestID, notificationIntent, PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.paw_icon)
                        .setContentTitle(title)
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(cont))
                        .setContentText(cont);
        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }
}

尝试以下操作:
public void createNotification(){
试一试{
Intent pnIntent=newintent(这是newActivity.class);
pnIntent.putExtra(“通知ID”,通知ID);
PendingEvent PendingEvent=PendingEvent.getActivity(this,01,pnIntent,Intent.FLAG_ACTIVITY_CLEAR_TASK);
NotificationCompat.Builder=新建NotificationCompat.Builder(getApplicationContext());
builder.setContentTitle(getString(R.string.app_name));
setContentText(getString(R.string.running));
builder.setContentIntent(挂起内容);
builder.setSmallIcon(R.drawable.ic_stat_永久通知);
builder.setAutoCancel(false);
builder.setcongressing(true);
设置优先级(1);
通知通知=builder.build();
final NotificationManager NotificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION\u服务);
NotificationManager.notify(永久通知ID,通知);
}捕获(例外e){
Log.e(“UnityApplication”,“创建通知时出错”,e);
}

}

当应用程序未处于活动状态时,
getApplicationContext()
将不可用,因此最好使用该服务的
上下文


我的建议是通过这些方法尽可能追溯到接收推送通知意图的位置,并更改传递给
onReceive()
方法的任何上下文,并将其替换为
getApplicationContext()
使用
context
,然后可以在方法中使用context参数。

Try而不是.getActivity(这,…Try.getActivity)(getBaseContext,…是的,我以前也有过,对我不起作用这方面有很多错误…例如Intent.FLAG\u ACTIVITY\u CLEAR\u任务有一个错误,基本上说我不能使用这个选项嘿,很遗憾我不明白你在说什么,能提供一些示例代码吗?抱歉!我正在用完整的c语言编辑帖子类的ode您可以发布您的服务代码吗?我将建议您更改什么添加该类的其余部分和接收类更改此Intent notificationIntent=new Intent(getApplicationContext(),WalkerRequest.class);更改为Intent notificationIntent=new Intent(this,WalkerRequest.class);
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);
    }
}