2013-04-23 139 views
0

我正在关注Google推送通知下面的教程。本教程使用WAMP,但我已经将它与SQL Server一起使用。一切正常。我可以从服务器上的php文件生成一条消息,并将消息发送给注册的电话。android推送通知打开浏览器

我的问题是消息是一个apk的URL,是应用程序的升级网址。当手机收到该消息时,该消息将处于通知绘制中,并在点击时消失。我想要发生的是当用户点击通知时,手机浏览器打开。这将开始下载新的应用程序。

如何让android打开浏览器如果消息是一个url?

由于提前,

马特

教程。

Tutorial.

/** 
    * 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 [email protected] 
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) 

回答

1

尝试这样的事情,当你点击你的通知,得到URL,并开始与Intent.ACTION_VIEW打算,而不是一个活动:

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(yourUrlInString)); 
startActivity(browserIntent); 
+0

嗨,我是m出现错误,表示没有找到处理意图的活动。我认为Intent.ACTION_VIEW会打开浏览器,如果一个网址传入? – turtleboy 2013-04-23 12:11:50

+0

我已经更新了帖子,以包括推动执行的代码 – turtleboy 2013-04-23 12:16:12

+0

对不起,发布错误以及 – turtleboy 2013-04-23 12:17:59

相关问题