簡體   English   中英

如何在適用於Android和Cordova的項目中單擊通知時顯示特定頁面?

[英]How to show a specific page on clicking a notification in a project that works with Android and cordova?

我在我的項目中使用cordova 3.3.1

我打算在Android設備的狀態欄通知中顯示通知。 為了實現這一點,我在GCMIntentService類中操縱了createNotification()函數,現在我收到了通知。

現在,我將在單擊通知時顯示特定頁面(取決於通知類型)。 您對此有任何想法嗎,我不知道該如何實施? 我只知道,如果我修改擴展CordovaActivity的java類,那么每當我運行“ cordova build”命令時,我都會丟失所有代碼,因為該類將由cordova生成。 這是我的代碼:

  --------------in my class GCMIntentService---------------------------
    public void createNotification(Context context, Bundle extras)
{   

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    String appName = getAppName(this);

    Intent notificationIntent = new Intent(this, G3Tracker.class);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    notificationIntent.putExtra("pushBundle", extras);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(context)
            .setDefaults(Notification.DEFAULT_ALL)
            .setSmallIcon(context.getApplicationInfo().icon)
            .setWhen(System.currentTimeMillis())
            .setContentTitle(extras.getString("title"))
            .setTicker(extras.getString("title"))
            .setContentIntent(contentIntent);

    String message = extras.getString("message");
    if (message != null) {
        mBuilder.setContentText("You recieved a new notification: " + message);

    } else {
        mBuilder.setContentText("message");

    }

 ----------------------------------------------------- 
            public class G3Tracker extends CordovaActivity 
 {
      @Override
       public void onCreate(Bundle savedInstanceState)
  {
         super.onCreate(savedInstanceState);
         super.init();
      // Set by <content src="index.html" /> in config.xml
         super.loadUrl(Config.getStartUrl());
       //super.loadUrl("file:///android_asset/www/index.html")
   }
 }
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notif = new Notification(com.project.R.drawable.app_icon, "heading title", System.currentTimeMillis());
CharSequence from = "application name";
CharSequence message = "message which you want to show";
intent = new Intent(MyActivity.this,ShowActivity.class);
intent.putExtra("NotifID", notifID);
pendingIntent= PendingIntent.getActivity(MyActivity.this, notifID, intent, PendingIntent.FLAG_UPDATE_CURRENT| PendingIntent.FLAG_ONE_SHOT);
notif.setLatestEventInfo(this, from, message, intent);
notif.flags |= Notification.FLAG_AUTO_CANCEL;
notif.vibrate = new long[] { 100, 250, 100, 500 };
nm.notify(notifID, notif);
finish();

嘗試使用此代碼對我有用,如果您有任何疑問,請回復我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM