帮助程序类中的通知发送方法
public static void createAndSendNotification(Context context, String title, int id, String text, PendingIntent pendingNotificationIntent) {
NotificationTarget mNotificationTarget;
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.remoteview_notification_updated);
// Bundle bndle = new Bundle();
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Random randomizer = new Random();
String pathforCategories = context.getSharedPreferences
(Constants.PACKAGE_NAME, 0).getString(Constants.DEFAULT_PATH, "")+title+"/" ;
Log.v("path_check", pathforCategories);
File f = new File(pathforCategories);
// List<File> filesL
File[] filesList = f.listFiles();
Log.v("reached", "here");
String type = new String();
HashMap <String,String> hm = new HashMap<>();
Gson gson = new Gson();
hm =gson.fromJson(context.getSharedPreferences(Constants.PACKAGE_NAME,Context.MODE_PRIVATE).
getString("textForNotification",""),HashMap.class);
Log.v("checkhm",hm+"");
List<File> filesAll = new ArrayList<>() ;
try {
filesAll = new ArrayList<>(Arrays.asList(filesList));
}
catch (NullPointerException ne)
{
Crashlytics.logException(ne);
}
Log.v("filessizw",String.valueOf(filesAll.size())+"si");
// Log.v("files",f.listFiles().toString()+"si2");
List<File> filesImages = new ArrayList<>();
if (filesAll.size() != 0) {
Log.v("1", "1");
for (int i = 0; i < filesAll.size(); i++) {
String extension = MimeTypeMap.getFileExtensionFromUrl(filesAll.get(i).getAbsolutePath());
Log.v("ext",extension);
if (extension.equalsIgnoreCase("jpg")) {
filesImages.add(filesAll.get(i));
}
// filesImages.get(i).del(ete();
else if (extension.equalsIgnoreCase("jpeg")) {
Log.v("1", "1");
filesImages.add(filesAll.get(i));
} else if (extension.equalsIgnoreCase("png")) {
Log.v("3", "3");
filesImages.add(filesAll.get(i));
}
else if (extension.equalsIgnoreCase("jpgm"))
{
Log.v("3", "3");
filesImages.add(filesAll.get(i));
}
}
//filesImages.notify();
Log.v("size images array", String.valueOf(filesImages.size()));
if (filesImages.size() != 0) {
File random = filesImages.get(randomizer.nextInt(filesImages.size()));
String realPath = random.getAbsolutePath();
File targetFile;
Uri uri;
try {
String pathfForImage = realPath;
/* String pathfForImage = Environment.getExternalStorageDirectory() +
File.separator + "Camera"+File.separator+"er.PNG";*/
targetFile = new File(pathfForImage);
;
Log.v("name for file", targetFile.getName());
uri = Uri.fromFile(targetFile);
Log.v("value of uri b", uri.toString());
} catch (NullPointerException e) {
Crashlytics.logException(e);
targetFile = new File("");
uri = Uri.fromFile(f);
Log.v("value of uri b", uri.toString());
}
Log.v("check_valueafter fetch",targetFile.getName().substring(6,10));
// Log.v("check_valueafter fetch",hm.get(uri.toString().substring(6,10)));
// rv.setTextViewText(R.id.txt_notification_headline, hm.get(uri.toString().substring(uri.toString().length()-5)));
// rv.setTextViewText(R.id.txt_notification_headline, "Reminder for Common Quotes");
// rv.setTextViewText(R.id.txt_notification_detail, text )
// rv.setImageViewResource(R.id.remoteview_notification_icon, R.mipmap.launcher);
Log.v("value of title", title);
Log.v("value of txt", text + id);
Log.v("value of uri af", uri.toString());
String textNotificationget=targetFile.getName().substring(6, 10); ;
try {
if (hm.containsKey(targetFile.getName().substring(6, 10))) {
}
}
catch (Exception ex)
{
ex.getMessage() ;
}
hm.get(textNotificationget) ;
try {
rv.setTextViewText(R.id.txt_notification_headline, hm.get(textNotificationget));
}
catch (Exception ex)
{
rv.setTextViewText(R.id.txt_notification_headline, "Common Quotes");
}
try {
rv.setImageViewBitmap(R.id.remoteview_notification_icon,MediaStore.Images.Media.getBitmap(context.getContentResolver(), uri));
//使用远程视图进行通知
}
catch(IOException ex)
{
ex.printStackTrace();
}
// rv.setImageViewUri(R.id.remoteview_notification_icon, uri);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
// rv.setImageViewBitmap(R.id.remoteview_notification_icon, BitmapFactory.decodeFile(targetFile.getAbsolutePath(),bmOptions));
// rv.setImageViewBitmap();
/* NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
*//* .setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)*//*
.setContentText(text+id);
final Notification notification = notificationBuilder.build();*/
通知代码
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.launcher)
.setContentTitle("Content Title")
.setContentText("Content Text").setAutoCancel(true)
.setContent(rv).setAutoCancel(true);
通知代码
final Notification notification = mBuilder.build();
// notification.
mNotificationTarget = new NotificationTarget(
context,
rv,
R.id.remoteview_notification_icon,
notification,
id);
/* try {
Glide
.with(context.getApplicationContext()) // safer!
.load(MediaStore.Images.Media.getBitmap(context.getContentResolver(), uri))
.asBitmap()
.into(mNotificationTarget);
}
catch(IOException ex)
{
ex.printStackTrace();
}*/
if (android.os.Build.VERSION.SDK_INT >= 16) {
notification.bigContentView = rv;
}
// rv.setViewVisibility(R.layout.remoteview_notification, View.GONE);
Log.v("code reached", "No");
mBuilder.setAutoCancel(true);
mBuilder.setContentIntent(pendingNotificationIntent);
Log.v("code reached", "Yes");
//NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
try {
rv.setOnClickPendingIntent(R.id.linearlayout_remote_view, pendingNotificationIntent);
Log.v("checknotifyActivity","yes");
} catch (Exception e) {
Crashlytics.logException(e);
}
// mBuilder.setAutoCancel(true);
mNotificationManager.notify(id, notification);
} else {
// Constants.showToastMessage(context, "There are no image files in selected path ");
// Log.v("check notifications", )
}
// mNotificationManager.cancel(id);
}
else {
// Constants.showToastMessage(context, "There are no files in selected path ");
}
}
GCM服务待定意图
在此处调用待处理的意图
Intent intentAction = new Intent(this, QuotesListActivity.class);
intentAction.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntentGCM = PendingIntent.getActivity(this,
1, intentAction, PendingIntent.FLAG_CANCEL_CURRENT);
快速帮助将不胜感激
提前致谢