通过单击服务的通知来恢复应用程序

时间:2014-09-10 22:20:24

标签: android notifications

我成功创建了一个启动服务的活动,而该服务又显示了这样的通知

public int onStartCommand(Intent intent, int flags, int startId){

String notificationText = "working"
        myNotification = new NotificationCompat.Builder(getApplicationContext())
                .setContentTitle("test")
                .setContentText(notificationText)
                .setTicker("Notification!")
                .setWhen(System.currentTimeMillis())
                .setDefaults(Notification.DEFAULT_SOUND)
                .setAutoCancel(true)
                .setSmallIcon(R.drawable.ic_launcher)
                .build();

//FOLLOWING TECHNIQUE IS DEPRECATED
 /* PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
    myNotification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); */

        myNotification.flags |= Notification.FLAG_NO_CLEAR;
        myNotification.flags = Notification.FLAG_ONGOING_EVENT;
        notificationManager.notify(1, myNotification);

        return START_STICKY; 
}

我知道还有其他与此相关的问题但他们都使用了setLatestEventInfo已被弃用

这就是我从另一项活动中所说的:

protected void beginBackgroundSer(){

        intentService = new Intent(this, Service.class);
        intentService.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        this.startService(intentService);

    }

因此,无论何时通过非弃用方法点击通知,我都希望恢复活动。非常感谢你的帮助。

2 个答案:

答案 0 :(得分:0)

来自documentations

  

public NotificationCompat.Builder setContentIntent(PendingIntent intent) - 提供PendingIntent以在单击通知时发送。如果您不提供意图,现在可以通过调用RemoteViews.setOnClickPendingIntent(int,PendingIntent)将PendingIntents添加到要启动的单个视图中。请务必阅读Notification.contentIntent以了解如何正确使用它。

因此您可以使用{/ 1}}方法,例如

setContentIntent(PendingIntent intent)

答案 1 :(得分:0)

您可以使用以下步骤创建通知。

  1. 为将来的参考创建通知ID,以便通知管理器更新通知。 // Notification ID to allow for future updates private static final int MY_NOTIFICATION_ID = 1;
  2. 为通知创建文本元素。

    // Notification Text Elements
    private final CharSequence tickerText = "This is a Really, Really, Super Long Notification Message!";
    private final CharSequence contentTitle = "Notification";
    private final CharSequence contentText = "You've Been Notified!";
    
  3. 定义通知操作元素,即当用户点击通知抽屉中的视图时会发生什么操作?我们使用意图来处理这个问题。

    // Notification Action Elements
    private Intent mNotificationIntent;
    private PendingIntent mContentIntent;
    
  4. 您还可以为此通知定义声音和振动。

    // Notification Sound and Vibration on Arrival
    private Uri soundURI = Uri
            .parse("android.resource://com.examples.Notification.StatusBarWithCustomView/"
                    + R.raw.alarm_rooster);
    private long[] mVibratePattern = { 0, 200, 200, 300 };
    
  5. 使用Notification.Builder类创建通知。在你的onCreate()

    中执行此操作
    mNotificationIntent = new Intent(getApplicationContext(),
            YourActivity.class);
    mContentIntent = PendingIntent.getActivity(getApplicationContext(), 0,
            mNotificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
    
    
                // Build the Notification
    
                Notification.Builder notificationBuilder = new Notification.Builder(
                        getApplicationContext())
                    .setTicker(tickerText)
                    .setSmallIcon(android.R.drawable.stat_sys_warning)
                    .setAutoCancel(true)
                    .setContentIntent(mContentIntent)
                    .setSound(soundURI)
                    .setVibrate(mVibratePattern)
                    .setContent(mContentView);
    
                // Pass the Notification to the NotificationManager:
                NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                mNotificationManager.notify(MY_NOTIFICATION_ID,
                        notificationBuilder.build());