0

I am tring to open an uploaded file through notifications, but it is not working, the notification appears but once I click on it, nothing happens.

This is the notification part of my code :

Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

Intent i = new Intent();      
i.setAction(android.content.Intent.ACTION_VIEW);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setDataAndType(Uri.parse("file://"+file_path), f.getString("type") );
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

PendingIntent contentIntent = PendingIntent.getActivity(Main.this, 0,  i , 0); 

mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder( BabupMain.this);
mBuilder.setContentTitle("test").addAction(android.R.drawable.alert_dark_frame, "hi",contentIntent) 
                                .setContentText("click to open the file")
                                .setLights(0xfff89829, 300, 5000)
                                .setOnlyAlertOnce(true)
                                .setSound(uri)
                                .setSmallIcon(android.R.drawable.ic_input_add);


mNotifyManager.notify( NOTIFICATION_ID , mBuilder.build());  
4

2 回答 2

0

我没有使用 NotificationCompat,所以不知道它是否这样做,但我创建了一个 Intent 然后不得不使用

PendingIntent contentIntent = PendingIntent.getActivity(this,0, i, PendingIntent.FLAG_CANCEL_CURRENT);

将打开文件的意图与通知联系起来

于 2013-04-05T19:11:01.820 回答
0

以您的方式设置 Intent可能会启动一个可以查看文件的应用程序,但如果没有应用程序与您的 Intent 匹配,则不会发生任何事情。

你有这段神秘的代码

{i.setDataAndType(Uri.parse("file://"+file_path), f.getString("type"))}

但没有提到“f”是什么,所以我不知道 {f.getString("type")) 是否会导致有效的 MIME 类型。

无论如何,这一切都是没有意义的。几乎在所有情况下,通知的内容意图都应该返回到创建通知的应用程序。这样做可以为用户提供最佳上下文。从应用程序中,允许用户发送另一个 Intent 来打开文件。

还要记住正确设置后堆栈。当前的实现不这样做,因此一旦查看文件,就会导致导航路径混乱。

于 2013-04-05T20:06:24.907 回答