推送Phonegap Build的通知

时间:2014-05-03 14:40:53

标签: android html cordova notifications

我有一个使用PhoneGap Build制作的HTML Android应用程序。

我希望Andrid用户每天在同一时间(例如10:00)收到通知栏上的推送通知(即使应用已关闭),其中包含&#34等文字打开我"。当我点击应用程序必须打开的通知时,我应该导航到我的某个应用程序页面。是否可以使用PhoneGap Notifications插件?或任何其他方式?

非常感谢!

2 个答案:

答案 0 :(得分:0)

1)在GCMIntentService类中:

    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)创建NotificationReceiver类:

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)使用特定网址创建NewActivity类:

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

答案 1 :(得分:0)

您可以启动后台服务,该服务仅包含定期创建和发送通知的线程。请参阅官方Android文档以了解雇用方法。

如果唯一的平台是Android,为什么要使用PhoneGap?对于单平台应用程序,使用专用SDK要好得多。相反,如果您需要将应用程序移植到iOS,则无法通过PhoneGap API管理通知,您需要使用本机编程。

最后我同意user.sc,我会在第一次通知中删除该应用程序:-)但我不想联合你这样做的原因