好的,我快到了。我收到了通知,但想打开应用程序并在您单击通知时在我拥有的 web 视图中加载一个 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)