从通知启动android扩展活动

时间:2018-06-24 00:25:13

标签: android notifications

我想从通知开始活动MovieNotifActivity 但它没有运行且什么也没显示

class MovieNotifActivity: MovieDetailActivity() {


@RequiresApi(Build.VERSION_CODES.O)
override fun onCreate(savedInstance: Bundle?){

        super.onCreate(savedInstance)
        Log.d("Salam","Test View ")
        val movie=intent.extras.get("movie") as Movie
        val intentNotif=MovieDetailActivity.createIntent(this,movie )
        addMovie(this,movie)

        val intent = MovieDetailActivity.createIntent(this, movie)
        val view = MovieFragment.newInstance(movie.getViewType())
        startNewActivityWithTransition(view.view!!, R.string.transition_movie_poster, intent)

}

}

这是MovieNotifActivity的代码 当我尝试从通知中启动MovieDetailActivity时,它工作正常,但是当我尝试此操作时,则无济于事 有什么我想念的东西或者忘记了吗? 谢谢您对我的帮助

这是我启动通知的代码

 fun addMovie(context: Context, movie: Movie){
    val channelId = "channel-01"
    val channelName = "SIL Channel"
    val importance = NotificationManager.IMPORTANCE_HIGH


    val testIntent = Intent(context, MovieNotifActivity::class.java)

    testIntent.putExtra("movie",movie!!)
    val pNotifIntent1 = PendingIntent.getActivity(context, 0, testIntent, PendingIntent.FLAG_ONE_SHOT)

    // Icones
    val icon1 = Icon.createWithResource(context, android.R.drawable.star_big_off)




    val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager


    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        val mChannel = NotificationChannel(
                channelId, channelName, importance)
        notificationManager.createNotificationChannel(mChannel)
    }

    val noti = Notification.Builder(context, channelId)
            .setContentTitle(movie.title)
            .setContentText(movie.overview)
            .setSmallIcon(icon1)
            .setContentIntent(pNotifIntent1)
            .setAutoCancel(true)
            .build()

    notificationManager.notify(0, noti)

}

1 个答案:

答案 0 :(得分:0)

使用这种方式从通知开始活动

    val notificationManager = context.getSystemService(
        Context.NOTIFICATION_SERVICE) as NotificationManager

    var resultIntent = Intent(this, ResultActivity.class);

    var stackBuilder = TaskStackBuilder.create(this)

    stackBuilder.addNextIntentWithParentStack(resultIntent);

    var resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
      val mChannel = NotificationChannel(
          channelId, channelName, importance)
      notificationManager.createNotificationChannel(mChannel)
    }

    val noti = Notification.Builder(context, channelId)
        .setContentTitle(movie.title)
        .setContentText(movie.overview)
        .setContentIntent(resultPendingIntent)
        .setSmallIcon(icon1)
        .setContentIntent(pNotifIntent1)
        .setAutoCancel(true)
        .build()

    notificationManager.notify(0, noti)