好的,我差不多了。我有通知但是想要打开应用程序并在单击通知时在webview中加载URL。我的代码如下。当我单击通知时,它会打开webview活动,但它会崩溃并且不会加载页面。
这是我的通知
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 :(得分:0)
您的问题是您正在从意图中读取字符串额外信息,但您永远不会在notificationIntent
中设置此字符串额外信息。
我怀疑你在下一行获得NullPointerException
:
if(intent.getStringExtra("type").equals("message"))
你真的需要打电话:
notificationIntent.putExtra( "type", "message" );
notificationIntent.putExtra( "mid", /* some String value */ );
在sendGCMIntent
方法中。