I believe that you have heard about the windows azure cloud platform more or less, and now the Internet has entered the cloud + end era. The PC tablet mobile phones in our hands are increasingly dependent on the network, in particular, some social networking applications require some information push. I have previously introduced Windows Phone push services to you, today, we will introduce the mobile PUSH Service Based on the Microsoft cloud platform.
First, you must install the Mobile Services SDK in addition to vs2012 + WP8 SDK.
First, we need to log on to the Management Portal windows azure Management page (of course you already have a Windows azure subscription ).
You can see the mobile service on the left or click the Add button in the lower left corner to create a new mobile service.
The wizard for creating a mobile service will pop up, enter your url to point to, and connect to the database. The last option is to select the location of your data center.
Of course, if you choose to use a new database, you must enter the database name, logon name, and password. Click Finish.
Then you can see the service you just created under the mobile service option.
Then you can downloadCodeSample project or add an existing project to mobile service. Here, I directly download the Demo code of Windows azure.
When you run your project, you can interact with mobile service. Is it easy?
In our service, you can directly browse the data in the data table.
Of course, the Demo code of Win8 is also downloaded here.
For pushing Windows Phone, there is no difference between such a client and the previous one. You still need to register a mobile push channel.
Mark push in the manifest File
Add the following code to the mobile app file:
1. Introduce the namespace
UsingMicrosoft. Phone. notification;
2. Add the following code:
Public Static Httpicationicationchannel currentchannel { Get ; Private Set ;} Private Void Acquirepushchannel () {currentchannel = Httpicationicationchannel. Find ( " Mypushchannel " ); If (Currentchannel = Null ) {Currentchannel = New Httpicationicationchannel ( " Mypushchannel " ); Currentchannel. open (); currentchannel. bindtoshelltile ();}}
3. Add method call in application_launching event Method
Acquirepushchannel ();
4. Add a field to the todoitem class
[Datamember (name ="Channel")]Public StringChannel {Get;Set;}
5. Finally, change it on the mainpage page.Buttonsave_clickEvent Response Code
Private VoidButtonsave_click (ObjectSender, routedeventargs e ){VaRTodoitem =NewTodoitem {text =Todoinput. Text, Channel=App. currentchannel. channeluri. tostring ()}; inserttodoitem (todoitem );}
On the Windows azure cloud, we want to edit the script code when inserting data.
SelectData-script-insert)
The update code is as follows:
FunctionInsert (item, user, request) {request.exe cute ({success:Function(){//Write to the response and then send the notification in the backgroundRequest. Respond (); push. mpns. sendfliptile (item. Channel, {Title: item. Text}, {success:Function(Pushresponse) {console. Log ("Sent push :", Pushresponse );}});}});}
In this case, we deploy our project to the simulator or mobile phone and ping the tile of our application to the desktop.
After inserting a piece of data, check that the tile icon has pushed a message.
In fact, the above is a quick guide on the windowsazure website. I want to summarize it for you, but I believe that you can not only use tile to push messages, but also use mobile to push messages on toast.
Mobile service not only supports Windows Phone, but alsoMessage push for Windows 8Next, I will introduce how to configure Mobile Service Message push for Windows 8.
Here I will use the todolist table just created in Windows azure not to create a database separately. You can also download the Demo code of Windows 8 from the Windows azure website with slight modifications to support Windows 8 Message push.
Windows 8 is more responsible for registration than Windows Phone. You must register in the Windows app store and getClient secret and package SidThe procedure is as follows:
First, you must log on to the submit an app page to register your Win8 application and reserve an application name for your application.
Subsequently, applications in the associated App Store in
Next, select Advanced features in Windows Dev center.
Select authenticating your service and record client secret and package security identifier (SID ).
Upload the record ID to push (in the push tag bar) in Windows azure ).
Of course, our windwos 8 application must also declare support for push services.
1. Use namespace
UsingWindows. Networking. pushnotifications;
2. Add code to the app file
Public StaticPushnotificationchannel currentchannel {Get;Private Set;}Private Async VoidAcquirepushchannel () {currentchannel=AwaitPushnotificationchannelmanager. createpushnotifchanchannelforapplicationasync ();}
3. Add in onlaunched event
Acquirepushchannel ();
4. Add attributes to the todoitem class
[Datamember (name ="Channel")]Public StringChannel {Get;Set;}
5. In mainpageAdd code to the buttonsave_click event
Private VoidButtonsave_click (ObjectSender, routedeventargs e ){VaRTodoitem =NewTodoitem {text = textinput. Text, channel =App. currentchannel. Uri}; inserttodoitem (todoitem );}
In addition, I will update the data insert script for Windows azure. I will push all device information when inserting data (Win8 & Windows Phone Tusi message)
Function Insert (Item1, user, request) {request.exe cute (); VaR Permissionstable = tables. gettable ('todoitem' ); Permissionstable. Where ( Function (){ Return This . Channel! = Null }). Read ({success: Function (Channels) {console. Log ( "Read data :" , Channels. Length ); VaR Pushstring = 'hello' ; Channels. foreach ( Function (Item ){ If (Item. iswin8data === True ) {Push. WNS. sendtoasttext04 (item. Channel, {text1: item1.text}, {success: Function (Pushresponse) {console. Log ( "Sent push :", Pushresponse );}});} Else {Push. mpns. sendtoast (item. Channel, {text1: item1.text, Test2: New Date (). toisostring (). Replace (/T/, ''). Replace (/\ .. + /,'' ), Param: '/Page2.xaml? Navigatedfrom = toast notification' }, {Success: Function (Pushresponse) {console. Log ( "Sent toast push sucess:", pushresponse + "" + Item. Channel) ;}, error: Function (Failedresponse) {console. Log ( "Sent toast push fail :" , Failedresponse );}});}});}});}
You are welcome to communicate with me or @ Wang Bo _ nick on Sina Weibo.