2014-12-05 54 views
0

我試過了大約20個不同的StackOverflow答案和NONE正在工作!PendingIntent沒有從通知中啓動

只想從我的通知中啓動一項活動。它不工作。它沒有做任何事情,如果我去我的主屏幕並點擊它,它甚至不會啓動應用程序。

我從一開始就添加了一堆東西,所以很抱歉所有可能甚至不需要的東西。我只是繼續尋找和嘗試解決方案。

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

嘗試而不是.getActivity(this,.....嘗試.getActivity(getBaseContext,..... – JLONG 2014-12-05 03:48:46

+0

是的,我以前也有過,不適合我 – user1610719 2014-12-05 04:16:49

回答

1

getApplicationContext()當你的應用程序不處於活動狀態時,將無法使用,所以更好使用該服務的context

我的建議是通過方法來在推送通知的意圖被接受的地方回來儘可能追查,並更改被向下傳遞到onReceive()方法不管上下文,並與getApplicationContext()context更換然後你可以使用方法中的上下文參數。

+0

嘿,我很遺憾不明白你在說什麼,謹慎提供一些示例代碼?對不起! – user1610719 2014-12-05 03:55:05

+0

我正在編輯帖子的類別的全部代碼 – user1610719 2014-12-05 03:56:05

+0

你可以發佈你的服務代碼,我會建議你改變什麼 – 2014-12-05 03:56:10

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

+0

有一堆錯誤這...例如Intent.FLAG_ACTIVITY_CLEAR_TASK有一個錯誤,基本上說我不能使用該選項 – user1610719 2014-12-05 03:50:49

+0

它在這裏工作。 – 2014-12-05 04:15:03

相關問題