我正在按照下面的 Google 推送通知教程进行操作。本教程使用 WAMP,但我已经让它与 SQL Server 一起使用。一切正常。我可以从服务器上的 php 文件生成一条消息,并将消息传递到注册的手机。
我遇到的问题是消息是应用程序升级 url 的 apk 的 URL。当手机收到消息时,消息在通知中,点击后消失。我想要发生的是当用户点击通知时,手机浏览器被打开。这将开始下载新应用程序。
如果消息是url,我怎样才能让android打开浏览器?
提前致谢,
马特
教程。
/**
* Issues a notification to inform the user that server has sent a message.
*/
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, PushTestActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
.
/**
* Receiving push messages
* */
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
// Waking up mobile if it is sleeping
WakeLocker.acquire(getApplicationContext());
/**
* Take appropriate action on this message
* depending upon your app requirement
* For now i am just displaying it on the screen
* */
if(newMessage != null){
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(newMessage));
startActivity(browserIntent);
}else{
Toast.makeText(getApplicationContext(), "New Message: " + newMessage, Toast.LENGTH_LONG).show();
}
// Showing received message
//lblMessage.append(newMessage + "\n");
//Toast.makeText(getApplicationContext(), "New Message: " + newMessage, Toast.LENGTH_LONG).show();
// Releasing wake lock
WakeLocker.release();
}
};
[编辑] 这是接收推送时执行的代码。它在 PushTestActivity 类中
04-23 13:12:52.630: E/AndroidRuntime(16231): FATAL EXCEPTION: main
04-23 13:12:52.630: E/AndroidRuntime(16231): java.lang.RuntimeException: Error receiving broadcast Intent { act=com.carefreegroup.pushtest.DISPLAY_MESSAGE flg=0x10 (has extras) } in com.carefreegroup.pushtest.PushTestActivity$1@412b29b0
04-23 13:12:52.630: E/AndroidRuntime(16231): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:846)
04-23 13:12:52.630: E/AndroidRuntime(16231): at android.os.Handler.handleCallback(Handler.java:615)
04-23 13:12:52.630: E/AndroidRuntime(16231): at android.os.Handler.dispatchMessage(Handler.java:92)
04-23 13:12:52.630: E/AndroidRuntime(16231): at android.os.Looper.loop(Looper.java:155)
04-23 13:12:52.630: E/AndroidRuntime(16231): at android.app.ActivityThread.main(ActivityThread.java:5485)
04-23 13:12:52.630: E/AndroidRuntime(16231): at java.lang.reflect.Method.invokeNative(Native Method)
04-23 13:12:52.630: E/AndroidRuntime(16231): at java.lang.reflect.Method.invoke(Method.java:511)
04-23 13:12:52.630: E/AndroidRuntime(16231): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
04-23 13:12:52.630: E/AndroidRuntime(16231): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)
04-23 13:12:52.630: E/AndroidRuntime(16231): at dalvik.system.NativeStart.main(Native Method)
04-23 13:12:52.630: E/AndroidRuntime(16231): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=null }
04-23 13:12:52.630: E/AndroidRuntime(16231): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1671)
04-23 13:12:52.630: E/AndroidRuntime(16231): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1542)
04-23 13:12:52.630: E/AndroidRuntime(16231): at android.app.Activity.startActivityForResult(Activity.java:3409)
04-23 13:12:52.630: E/AndroidRuntime(16231): at android.app.Activity.startActivityForResult(Activity.java:3370)
04-23 13:12:52.630: E/AndroidRuntime(16231): at android.app.Activity.startActivity(Activity.java:3580)
04-23 13:12:52.630: E/AndroidRuntime(16231): at android.app.Activity.startActivity(Activity.java:3548)
04-23 13:12:52.630: E/AndroidRuntime(16231): at com.carefreegroup.pushtest.PushTestActivity$1.onReceive(PushTestActivity.java:142)
04-23 13:12:52.630: E/AndroidRuntime(16231): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:832)