Andriod Phonegap system push

Source: Internet
Author: User

I spent a lot of time researching phonegap, hoping to find a push method that can replace round robin. After trying pushwoosh, I found two problems. One is that the mobile phone must be integrated with the andriod apis component, secondly, I don't know why there is always an account failure error. There is really no way to solve it. Even if you solve it, the first problem is still hard to solve.
Later, at the recommendation of jiangge, he found a phonegap plug-in pushed by the system.
Well, let's first introduce the usage: www.2cto.com
1. (two key files (. java and. js components ))
[Java]
/*
*
* Copyright (C) 2011 Dmitry Savchenko <dg.freak@gmail.com>
*
* Permission is hereby granted, free of charge, to any person
* Obtaining a copy of this software and associated documentation
* Files (the "Software"), to deal in the Software
* Restriction, including without limitation the rights to use,
* Copy, modify, merge, publish, distribute, sublicense, and/or merge
* Copies of the Software, and to permit persons to whom
* Software is furnished to do so, subject to the following
* Conditions:
*
* The above copyright notice and this permission notice shall be
* Included in all copies or substantial portions of the Software.
*
* The software is provided "as is", without warranty of any kind,
* Express or implied, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* Of merchantability, fitness for a special PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* Holders be liable for any claim, damages or other liability,
* Whether in an action of contract, tort or otherwise, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* Other dealings in the software.
*
*/
 
Package com. tricedesigns;
 
Import org. apache. cordova. api. PluginResult. Status;
Import org. json. JSONArray;
Import org. json. JSONException;
 
Import android. app. Notification;
Import android. app. icationicationmanager;
Import android. app. PendingIntent;
Import android. content. Context;
Import android. content. Intent;
Import android. util. Log;
 
Import com. phonegap. api. Plugin;
Import com. phonegap. api. PluginResult;
 
 
Public class StatusBarNotification extends Plugin {
// Action to execute
Public static final String policy = "policy ";
Public static final String CLEAR = "clear ";

/**
* Executes the request and returns PluginResult
*
* @ Param action Action to execute
* @ Param data JSONArray of arguments to the plugin
* @ Param callbackId The callback id used when calling back into JavaScript
*
* @ Return A PluginRequest object with a status
**/
@ Override
Public PluginResult execute (String action, JSONArray data, String callbackId ){
String ns = Context. icationication_service;
Micationicationmanager = (NotificationManager) ctx. getSystemService (ns );
Context = ctx. getApplicationContext ();

PluginResult result = null;
If (policy. equals (action )){
Try {
 
String title = data. getString (0 );
String body = data. getString (1 );
Log. d ("icationicationplugin", "Notification:" + title + "," + body );
ShowNotification (title, body );
Result = new PluginResult (Status. OK );
} Catch (JSONException jsonEx ){
Log. d ("icationicationplugin", "Got JSON Exception"
+ JsonEx. getMessage ());
Result = new PluginResult (Status. JSON_EXCEPTION );
}
} Else if (CLEAR. equals (action )){
ClearNotification ();
} Else {
Result = new PluginResult (Status. INVALID_ACTION );
Log. d ("icationicationplugin", "Invalid action:" + action + "passed ");
}
Return result;
}
 
/**
* Displays status bar notification
*
* @ Param contentTitle Notification title
* @ Param contentText Notification text
**/
Public void showNotification (CharSequence contentTitle, CharSequence contentText ){
Int icon = R. drawable. nofication;
Long when = System. currentTimeMillis ();

Notification notification = new Notification (icon, contentTitle, when );
// Notification. flags | = Notification. FLAG_NO_CLEAR; // Notification cannot be clearned by user

Intent icationicationintent = new Intent (Context) ctx, ctx. getClass ());
PendingIntent contentIntent = PendingIntent. getActivity (Context) ctx, 0, icationicationintent, 0 );
Notification. setLatestEventInfo (context, contentTitle, contentText, contentIntent );

Micationicationmanager. Y (1, notification );
}

/**
* Removes the Notification from status bar
*/
Public void clearNotification (){
Micationicationmanager. cancelAll ();
}

Private icationicationmanager micationicationmanager;
Private Context context;
}


. Js File
[Javascript]
/*
*
* Copyright (C) 2011 Dmitry Savchenko <dg.freak@gmail.com>
*
* Permission is hereby granted, free of charge, to any person
* Obtaining a copy of this software and associated documentation
* Files (the "Software"), to deal in the Software
* Restriction, including without limitation the rights to use,
* Copy, modify, merge, publish, distribute, sublicense, and/or merge
* Copies of the Software, and to permit persons to whom
* Software is furnished to do so, subject to the following
* Conditions:
*
* The above copyright notice and this permission notice shall be
* Included in all copies or substantial portions of the Software.
*
* The software is provided "as is", without warranty of any kind,
* Express or implied, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* Of merchantability, fitness for a special PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* Holders be liable for any claim, damages or other liability,
* Whether in an action of contract, tort or otherwise, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* Other dealings in the software.
*
*/

/**
*
* Constructor
*/
Var icationicationmessenger = {
Y: function (title, body ){
Return cordova.exe c (null, null, 'statusbarnotification', 'weighy', [title, body]);
},
Clear: function (){
Return cordova.exe c (null, null, 'statusbarnotification', 'clear', []);
},



}


3.remember to put an image of icationication.png in the drawable folder under res.
4. Modify plugin. xml (add a record)
[Html]
<Plugin name = "StatusBarNotification" value = "com. tricedesigns. StatusBarNotification"/>

5. Define the called Function
[Javascript]
Function aaa (){
Icationicationmessenger. notify
}

6. OK. Find a button to trigger this aaa function. Then, run it and you will see the following results:

Author: xiaoguang44

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.