2014-05-03 4 views
0

J'ai une application Android HTML faite avec PhoneGap Build. Je souhaite qu'un utilisateur Andrid reçoive une notification push sur la barre de notification (même si l'application est fermée) tous les jours en même temps (par exemple à 10:00) avec le texte comme "ouvrez-moi" . Lorsque je clique sur la notification que l'application doit ouvrir, je devrais être redirigé vers l'une des pages de mon application. Est-il possible de faire avec le plugin PhoneGap Notifications? ou de toute autre manière?Notifications push pour Phonegap Build

Merci beaucoup!

+1

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. –

Répondre

0

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");  
}} 
0

Vous pouvez lancer une service d'arrière-plan qui inclut uniquement un thread qui crée et envoie périodiquement une notification. Reportez-vous à la documentation officielle Android pour comprendre l'embauche pour le faire. Pourquoi utilisez-vous PhoneGap si la seule plate-forme sera Android? Pour une application à plateforme unique, il est préférable d'utiliser un SDK dédié. Au contraire, si vous avez besoin de porter votre application sur iOS, les notifications ne peuvent pas être gérées via l'API PhoneGap, vous devez utiliser la programmation native.

Enfin, je suis d'accord avec user.sc, je supprimerais l'application à la première notification :-) Mais je ne veux pas syndiquer vos raisons de le faire