2013-04-15 66 views
0

好吧,我快到了。我收到了通知,但想要打開該應用並在單擊通知時所用的web視圖中加載一個URL。我擁有的代碼如下。當我點擊通知時,它會打開webview活動,但它會崩潰並且不會加載頁面。從通知中打開newInent和loadUrl

這是我的通知

private void sendGCMIntent(final Context theContext, Intent theMsg) 
    { 
     int icon = R.drawable.noti_msg, count; 
     long when = Calendar.getInstance().getTimeInMillis(); 

     String msg = theMsg.getStringExtra("message"); 
     String url = theMsg.getStringExtra("photo"); 
     count = Integer.parseInt(theMsg.getStringExtra("count")); 
     Bitmap photo = getBitmapFromURL(url); 
     String nameEncode = theMsg.getStringExtra("name"); 

     String theMessage = Html.fromHtml(msg).toString().replace("\\", ""); 
     String name = Html.fromHtml(nameEncode).toString(); 

     String title = theContext.getString(R.string.app_name); 
     Uri soundUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.msg); 

     long[] vibraPattern = { 0, 500, 250, 500 }; 

     Intent notificationIntent = new Intent(theContext,MainActivity.class); 
     notificationIntent.setData(Uri.parse("content://"+when)); 

     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     notificationIntent.addCategory("android.intent.category.LAUNCHER"); 
     PendingIntent contentIntent = PendingIntent.getActivity(theContext, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); 

     NotificationManager notificationManager = (NotificationManager) theContext.getSystemService(Context.NOTIFICATION_SERVICE); 

     NotificationCompat.Builder notification = new NotificationCompat.Builder(theContext); 

     notification.setContentTitle(title); 
     notification.setContentText("New message from: "+ name); 
     notification.setSubText(theMessage); 
     notification.setContentIntent(contentIntent); 
     notification.setTicker("New message"); 
     notification.setNumber(count); 
     notification.setSmallIcon(icon); 
     notification.setLargeIcon(photo); 
     notification.setAutoCancel(true); 
     notification.setSound(soundUri); 
     notification.setVibrate(vibraPattern); 
     notification.setWhen(when); 

     Notification notifies = notification.build(); 

     notificationManager.notify((int)when, notifies);  
    } 

而在我的MainActivity我有這個

@Override 
    protected void onNewIntent(Intent intent) { 
     super.onNewIntent(intent); 
     if(intent.getStringExtra("type").equals("message")) 
     { 
      String mUrl = intent.getStringExtra("mid"); 
      webView.loadUrl(mUrl); 
     } 
    } 

下面是錯誤日誌

04-15 07:33:41.061: E/AndroidRuntime(19851): FATAL EXCEPTION: main 
04-15 07:33:41.061: E/AndroidRuntime(19851): java.lang.NullPointerException 
04-15 07:33:41.061: E/AndroidRuntime(19851): at me.site.test.MainActivity.onNewIntent(MainActivity.java:230) 
04-15 07:33:41.061: E/AndroidRuntime(19851): at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1168) 
04-15 07:33:41.061: E/AndroidRuntime(19851): at android.app.ActivityThread.deliverNewIntents(ActivityThread.java:2181) 
04-15 07:33:41.061: E/AndroidRuntime(19851): at android.app.ActivityThread.performNewIntents(ActivityThread.java:2194) 
04-15 07:33:41.061: E/AndroidRuntime(19851): at android.app.ActivityThread.handleNewIntent(ActivityThread.java:2203) 
04-15 07:33:41.061: E/AndroidRuntime(19851): at android.app.ActivityThread.access$1500(ActivityThread.java:139) 
04-15 07:33:41.061: E/AndroidRuntime(19851): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1295) 
04-15 07:33:41.061: E/AndroidRuntime(19851): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-15 07:33:41.061: E/AndroidRuntime(19851): at android.os.Looper.loop(Looper.java:137) 
04-15 07:33:41.061: E/AndroidRuntime(19851): at android.app.ActivityThread.main(ActivityThread.java:4918) 
04-15 07:33:41.061: E/AndroidRuntime(19851): at java.lang.reflect.Method.invokeNative(Native Method) 
04-15 07:33:41.061: E/AndroidRuntime(19851): at java.lang.reflect.Method.invoke(Method.java:511) 
04-15 07:33:41.061: E/AndroidRuntime(19851): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004) 
04-15 07:33:41.061: E/AndroidRuntime(19851): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771) 
04-15 07:33:41.061: E/AndroidRuntime(19851): at dalvik.system.NativeStart.main(Native Method) 
+0

請包括您的logcat輸出。你說它崩潰了,但是你不會向我們展示它在崩潰時拋出的異常。 –

回答

0

你的問題是,你正在閱讀的String你的目的是額外的,但你從來沒有在notificationIntent中設置這個String Extra。

我懷疑你在下面的行得到一個NullPointerException

if(intent.getStringExtra("type").equals("message")) 

你真的需要被調用:

notificationIntent.putExtra("type", "message"); 
notificationIntent.putExtra("mid", /* some String value */); 
sendGCMIntent方法

+0

是的,它是一個nullpointerException。好的,我需要改變onNewIntent方法?我不知道中期的價值,那麼如何在我的通知方法中提取該數據呢? –

+0

我採取了回來我沒有得到在我的通知方法中的字符串,但我確實得到消息。我需要能夠檢查它並獲得中間的價值。 –

+0

剛接觸Java/Android時,我花了一段時間才明白你的意思。但是這似乎解決了問題。有時新手需要更多解釋。 :) –