简体   繁体   中英

Notifications won't open file

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());  

I haven't used NotificationCompat, so don't know if it does this, but I created an Intent and then had to use

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

to tie the intent of opening your file with the notification

Setting the Intent the way you did may start an app that can view files, but if no app matches your Intent, nothing will happen.

You have this mysterious piece of code

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

but no mention of what "f" is, so I can't tell if {f.getString("type")) results in a valid MIME type.

All this is moot anyway. A notification's content Intent should, in nearly all cases, go back to the app that created the notification. Doing this provides the best context to the user. From the application , allow the user to send another Intent to open the file.

Remember also to set the back stack correctly. The current implementation doesn't do this, so it results in a confusing navigation path once the the file is viewed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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