【问题标题】:start an activity when clicking on notifications单击通知时开始活动
【发布时间】:2013-03-19 12:06:45
【问题描述】:

我使用此代码发出通知:

NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//Create your notification
int icon = R.drawable.ic_launcher;
CharSequence tickerText = " message";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText,when);
Context context = getApplicationContext();
CharSequence contentTitle = last_notifs_array[0][2];
CharSequence contentText = "New Message";
PendingIntent pIntent = PendingIntent.getActivity(notifService.this,0, intent1,0);
notification.setLatestEventInfo(context,contentTitle,contentText,pIntent);
// Send the notification
nm.notify(HELLO_ID,notification);

但我想在单击通知时开始一项活动。我怎样才能做到这一点?

【问题讨论】:

标签: java android notifications


【解决方案1】:

试试这个:代替 YourActivity pur 你想要调用的活动

NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
         // Create your notification
         int icon = R.drawable.ic_launcher;
         CharSequence tickerText = " message";
         Intent notificationIntent
         long when = System
                 .currentTimeMillis();
         Notification notification = new Notification(
                 icon, tickerText,
                 when);
         notificationIntent = new Intent(context,
                    YourActivity.class);
         Context context = getApplicationContext();
         CharSequence contentTitle = last_notifs_array[0][2];
         CharSequence contentText = "New Message";
         PendingIntent pIntent = PendingIntent
                 .getActivity(
                         notifService.this,
                         0, notificationIntent,
                         0);
         notification
                 .setLatestEventInfo(
                         context,
                         contentTitle,
                         contentText,
                         pIntent);
         // Send the notification
         nm.notify(HELLO_ID,
                 notification);

【讨论】:

    【解决方案2】:

    你可以用这个

        final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        final Notification notification = new Notification(R.drawable.notification_popup, message, System.currentTimeMillis());
        final PendingIntent contentIntent = PendingIntent.getActivity(context, 0, 
                new Intent(context, YourActivity.class), Notification.FLAG_AUTO_CANCEL);
    
        notification.setLatestEventInfo(context, title, message, contentIntent);
        notification.defaults = Notification.DEFAULT_ALL;
        notificationManager.notify(yourId, notification);
    

    应该可以解决你的问题

    【讨论】:

      【解决方案3】:

      使用此代码,这将像一个魅力。

       NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
              Notification notification = new Notification(icon, message, when);
              String title = "Your application name with notify string.";
              Intent notificationIntent = new Intent(context, GoogleMapActivity.class);
      
              notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
              PendingIntent intent =PendingIntent.getActivity(context, 0, notificationIntent, 0);
              notification.setLatestEventInfo(context, title, message, intent);
              notification.flags |= Notification.FLAG_ONGOING_EVENT;
              notification.flags |= Notification.FLAG_NO_CLEAR;
              notification.flags |= Notification.FLAG_SHOW_LIGHTS;
              notification.flags |= Notification.DEFAULT_LIGHTS;
                      notificationManager.notify(0, notification);
      

      【讨论】:

        【解决方案4】:

        声明一个 PendingIntent:

        Intent intent = ... create intent of your activity here ....
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        

        然后只需将参数传递给您的notification.pendingIntent = contentIntent;

        【讨论】:

          【解决方案5】:

          使用下面提到的Intent setClass 方法。

          Intent notifyIntent = new Intent(Intent.ACTION_MAIN);
          notifyIntent.setClass(getApplicationContext(), Main.class);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-09-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多