2013-04-23 134 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

相關問題