【问题标题】:Launch app from Notification Bar从通知栏启动应用程序
【发布时间】:2012-11-12 10:52:45
【问题描述】:

我正在使用 phonegap 开发一个 android 应用程序。我想从通知栏启动应用程序。虽然这可能是一个重复的问题,但似乎没有什么对我有用;我尝试过的链接很少。

http://pilhuhn.blogspot.in/2010/12/pitfall-in-pendingintent-with-solution.html

Open android app from PUSH notification

re-open background application via notification item

从此链接开发的独立应用程序正在运行,但是当我与我的真实项目集成时,点击通知栏什么也没做,下面是我的代码

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

Notification notification = new Notification(R.drawable.ic_launcher,"Message received", System.currentTimeMillis());
        notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;

Intent intent = new Intent(context, MyClass.class);

intent.putExtra("message", message);
intent.putExtra("shortMsg", shortMsg);
intent.putExtra("source", source);
intent.putExtra("phone", phone);
intent.putExtra("datetime", datetime);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

context.startActivity(intent);

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        notification.setLatestEventInfo(context, "Message", "New message received", pendingIntent);

notificationManager.notify(0, notification);

MyClass.class 代码,

Bundle extras = getIntent().getExtras();
if (extras != null) 
{
    System.out.println("in extras");            
    //Retrive data from the intent and store them in instance variables
    this.message = extras.getString("message");
    this.shortMsg = extras.getString("shortMsg");
    this.source = extras.getString("source");
    this.phone = extras.getString("phone");
    this.datetime = extras.getString("datetime");
}

提前致谢,

七无

【问题讨论】:

    标签: android cordova android-notifications android-notification-bar


    【解决方案1】:

    我使用NotificationCompat.Builder,因为它更容易实现。

    Intent intent = new Intent(context, Main.class);
    PendingIntent pintent = PendingIntent.getActivity(context, 0, intent, 0);
    mNotification = new NotificationCompat.Builder(context)
          .setContentTitle(context.getString(R.string.app_name))
          .setContentText(message)
          .setContentIntent(pintent)
          .setSmallIcon(R.drawable.ic_notification)
          .setWhen(System.currentTimeMillis())
          .setAutoCancel(cancellable ? true : false)
          .setOngoing(cancellable ? false : true)
          .build();
    notificationManager.notify(0, mNotification);
    

    【讨论】:

    • 我认为您不能将任何键值对放入待处理的意图中。即intent.putExtra("message",message);这条线是什么 NotificationCompat.Builder(context);有那个名字的类吗???
    • 是的,您对键值对的看法是正确的。我的监督。 NotificationCompat.Builder(context) 在支持库中。
    【解决方案2】:

    使用 setContentIntent 并将待处理的意图作为参数传递。

    Intent intent = new Intent(context, MyClass.class);
    intent.putExtra("message", message);
    intent.putExtra("shortMsg", shortMsg);
    intent.putExtra("source", source);
    intent.putExtra("phone", phone);
    intent.putExtra("datetime", datetime);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pintent = PendingIntent.getActivity(context, 0, intent, 0);
    
    mNotification = new NotificationCompat.Builder(context)
          .setContentTitle(context.getString(R.string.app_name))
          .setContentText(message)
          .setContentIntent(pintent)
          .setSmallIcon(R.drawable.ic_notification)
          .setContentIntent(pintent)
          .build();
    notificationManager.notify(0, mNotification);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-06
      • 1970-01-01
      相关资源
      最近更新 更多