简体   繁体   English

我如何获得通知意图数据

[英]How Do I get Notification Intent Data

I want to show a notification at some event, that's working fine, I am also landing on an activity I want but problem is that, intent data is empty, please here, Here is the code 我想在某个事件中显示一个通知,它工作正常,我也正在进行我想要的活动,但是问题是,意图数据为空,请在这里,这里是代码

    Intent resultIntent = new Intent(context, MovieDetailActivity.class);
        resultIntent.putExtra(Constants.MOVIE_ID, cursor.getString(
                cursor.getColumnIndexOrThrow(DatabaseHelper.ID)));
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addNextIntentWithParentStack(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_download)
                .setContentIntent(resultPendingIntent)
                .setContentTitle(cursor.getString(
                        cursor.getColumnIndexOrThrow(DatabaseHelper.NAME)))
                .setAutoCancel(true)
                .setContentText(cursor.getString(
                        cursor.getColumnIndexOrThrow(DatabaseHelper.REALEASE_DATE)));
        NotificationManager notificationManager = (NotificationManager)
                context.getSystemService(context.NOTIFICATION_SERVICE);

        notificationManager.notify(1, builder.build()
        );

I am sending some data using intent, in this line. 我在这一行中使用意图发送一些数据。

resultIntent.putExtra(Constants.MOVIE_ID, cursor.getString(
                cursor.getColumnIndexOrThrow(DatabaseHelper.ID)));

I am receiving data here but it Null. 我在这里接收数据,但它为空。

Intent receivedIntent = getIntent();
    mMovieId = receivedIntent.getIntExtra(Constants.MOVIE_ID, -1);

Please help, I have spent whole day on this. 请帮助,我已经整整一整天在这上面了。

You pass into intent String so you can't get Integer from intent, that's why you get null. 您将intent传递给String,这样就无法从intent中获取Integer,这就是为什么您会得到null的原因。

So change this line: 因此更改此行:

mMovieId = receivedIntent.getIntExtra(Constants.MOVIE_ID, -1);

to

 mMovieId = receivedIntent.getStringExtra(Constants.MOVIE_ID);  
// and simply parse string to integer  
       int id = Integer.parseInt(mMovieId);

You need change your PendingIntent for: 您需要更改PendingIntent用于:

PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0 , resultIntent, PendingIntent.FLAG_CANCEL_CURRENT);

I hope it solve your problem. 希望它能解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM