尚未从通知启动PendingIntent

时间:2014-12-05 03:42:59

标签: android

我已经尝试了大约20种不同的StackOverflow答案,而NONE正在运行!

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

我从一开始就添加了很多东西,很抱歉所有可能都不需要的东西。我只是一直在寻找SO并尝试解决方案。

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:-1)

试试这个:public void createNotification() { try { Intent pnIntent = new Intent(this, newActivity.class); pnIntent.putExtra("notificationId", NOTIFICATION_ID); PendingIntent pendingIntent = PendingIntent.getActivity(this, 01, pnIntent, Intent.FLAG_ACTIVITY_CLEAR_TASK); NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext()); builder.setContentTitle(getString(R.string.app_name)); builder.setContentText(getString(R.string.running)); builder.setContentIntent(pendingIntent); builder.setSmallIcon(R.drawable.ic_stat_permanent_notification); builder.setAutoCancel(false); builder.setOngoing(true); builder.setPriority(1); Notification notification = builder.build(); final NotificationManager notificationManger = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManger.notify(permanentNotificationId, notification); } catch (Exception e) { Log.e("UnityApplication", "Error while creating notification", e); } }