1) GCMIntentService Classe:
private void handleMessage(Context context, Intent intent) {
Log.i(TAG, "handleMessage");
// TODO Auto-generated method stub
String notiMsg = intent.getStringExtra("msg");
String message = notiMsg;`enter code here`
long when = System.currentTimeMillis();
int iwhen = (int)when;
// define sound URI, the sound to be played when there's a notification
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent notificationIntent = new Intent(this.getApplicationContext(), NotificationReceiver.class);
PendingIntent pIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, notificationIntent,0);
// this is it, we'll build the notification!
// in the addAction method, if you don't want any icon, just set the first param to 0
NotificationCompat.Builder mNotification = new NotificationCompat.Builder(this)
.setContentTitle("Title")
.setContentText(message)
.setSmallIcon(R.drawable.ic_launcher)
.setSound(soundUri)
.setWhen(when)
.setAutoCancel(true)
.setContentIntent(pIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(iwhen, mNotification.build());
}
2) Créer NotificationReceiver Classe:
public class NotificationReceiver extends DroidGap {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();
super.loadUrl("file:///android_asset/www/index.html");
AlertDialog.Builder alert_box=new AlertDialog.Builder(this);
alert_box.setTitle("Title");
alert_box.setMessage("New message received, would you like to open it.");
alert_box.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), "Yes Button Clicked",Toast.LENGTH_LONG).show();
login();
}
});
alert_box.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), "No Button Clicked", Toast.LENGTH_LONG).show();
}
});
alert_box.show();
if (Context.NOTIFICATION_SERVICE!=null) {
Log.i("NotificationReceiver","Clear all notification");
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager) getApplicationContext().getSystemService(ns);
nMgr.cancelAll();
}
}
public void login() {
Intent intent = new Intent(NotificationReceiver.this, NewActivity.class);
startActivity(intent);
}
@Override
public void onBackPressed() {
Log.i("NotificationReceiver","onB`enter code here`ackPressed");
}}
3) Créer NewActivity classe avec l'URL spécifique:
public class NewActivity extends DroidGap {
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i("NewActivity","NewActivityAAAAAAAAAAAA");
super.onCreate(savedInstanceState);
super.init();
super.loadUrl("file:///android_asset/www/notification_main.html");
}
@Override
public void onBackPressed() {
Log.i("NewActivity","onBackPressed");
}}
Sauf si vous avez un * vraiment * bonne raison pour ceci, s'il vous plaît ** ne pas **. À moins qu'il s'agisse d'une application qui génère des alarmes ou qui est requise pour une application d'entreprise qui n'est pas distribuée au public, cela est considéré comme un comportement de type spam. Dieu sait que les applications qui le font ne durent pas longtemps sur mon appareil. –