Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@
String ticker = "您有一条新通知";
String title = "冰冰";
ArrayList<String> messageList = new ArrayList<String>();
messageList.add("文明,今晚有空吗?");
messageList.add("Kelly,今晚有空吗?");
messageList.add("晚上跟我一起去玩吧?");
messageList.add("怎么不回复我?我生气了!!");
messageList.add("怎么不回复我?我生气了!!");
messageList.add("我真生气了!!!!!你听见了吗!");
messageList.add("文明,别不理我!!!");
messageList.add("Kelly,别不理我!!!");
String content = "[" + messageList.size() + "条]" + title + ": " + messageList.get(0);
//实例化工具类,并且调用接口
NotifyUtil notify3 = new NotifyUtil(mContext, 3);
Expand Down Expand Up @@ -262,7 +262,7 @@
int largeIcon = R.drawable.fbb_largeicon;
String ticker = "您有一条新通知";
String title = "范冰冰";
String content = "文明,今晚在希尔顿酒店2016号房哈";
String content = "kelly,今晚在希尔顿酒店2016号房哈";
Intent intent = new Intent(mContext, OtherActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(mContext,
Expand Down
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
classpath 'com.android.tools.build:gradle:3.1.3'

//
}
Expand All @@ -14,6 +15,7 @@ buildscript {
allprojects {
repositories {
jcenter()
google()
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
10 changes: 5 additions & 5 deletions library/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
compileSdkVersion 26
buildToolsVersion "26.0.3"

defaultConfig {
minSdkVersion 14
targetSdkVersion 23
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
}
Expand All @@ -21,5 +21,5 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:appcompat-v7:26.1.0'
}
67 changes: 37 additions & 30 deletions library/src/main/java/com/wenming/library/NotifyUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
Expand All @@ -24,7 +25,7 @@ public class NotifyUtil {
private int NOTIFICATION_ID;
private NotificationManager nm;
private Notification notification;
private NotificationCompat.Builder cBuilder;
private Notification.Builder cBuilder;
private Notification.Builder nBuilder;
private Context mContext;

Expand All @@ -33,9 +34,21 @@ public NotifyUtil(Context context, int ID) {
this.NOTIFICATION_ID = ID;
mContext = context;
// 获取系统服务来初始化对象
nm = (NotificationManager) mContext
.getSystemService(Activity.NOTIFICATION_SERVICE);
cBuilder = new NotificationCompat.Builder(mContext);
nm = (NotificationManager) mContext.getSystemService(Activity.NOTIFICATION_SERVICE);
cBuilder = new Notification.Builder(mContext);

if (Build.VERSION.SDK_INT >= 26) {
//当sdk版本大于26
String id = "channel_1";
String description = "143";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel channel = new NotificationChannel(id, description, importance);
channel.enableLights(true);
channel.enableVibration(true);
nm.createNotificationChannel(channel);
cBuilder = new Notification.Builder(mContext, id);
cBuilder.setCategory(Notification.CATEGORY_MESSAGE);
}
}

/**
Expand All @@ -62,25 +75,25 @@ private void setCompatBuilder(PendingIntent pendingIntent, int smallIcon, String
cBuilder.setContentText(content);// 设置通知中心中的内容
cBuilder.setWhen(System.currentTimeMillis());

/*
/*
* 将AutoCancel设为true后,当你点击通知栏的notification后,它会自动被取消消失,
* 不设置的话点击消息后也不清除,但可以滑动删除
*/
* 不设置的话点击消息后也不清除,但可以滑动删除
*/
cBuilder.setAutoCancel(true);
// 将Ongoing设为true 那么notification将不能滑动删除
// notifyBuilder.setOngoing(true);
/*
* 从Android4.1开始,可以通过以下方法,设置notification的优先级,
* 优先级越高的,通知排的越靠前,优先级低的,不会在手机最顶部的状态栏显示图标
*/
cBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
* 优先级越高的,通知排的越靠前,优先级低的,不会在手机最顶部的状态栏显示图标
*/
cBuilder.setPriority(Notification.PRIORITY_MAX);
/*
* Notification.DEFAULT_ALL:铃声、闪光、震动均系统默认。
* Notification.DEFAULT_SOUND:系统默认铃声。
* Notification.DEFAULT_VIBRATE:系统默认震动。
* Notification.DEFAULT_LIGHTS:系统默认闪光。
* notifyBuilder.setDefaults(Notification.DEFAULT_ALL);
*/
* Notification.DEFAULT_SOUND:系统默认铃声。
* Notification.DEFAULT_VIBRATE:系统默认震动。
* Notification.DEFAULT_LIGHTS:系统默认闪光。
* notifyBuilder.setDefaults(Notification.DEFAULT_ALL);
*/
int defaults = 0;

if (sound) {
Expand Down Expand Up @@ -116,7 +129,7 @@ private void setBuilder(PendingIntent pendingIntent, int smallIcon, String ticke

nBuilder.setTicker(ticker);
nBuilder.setWhen(System.currentTimeMillis());
nBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
nBuilder.setPriority(Notification.PRIORITY_MAX);

int defaults = 0;

Expand Down Expand Up @@ -176,7 +189,6 @@ public void notify_mailbox(PendingIntent pendingIntent, int smallIcon, int large
PendingIntent deletePendingIntent = PendingIntent.getService(mContext,
deleteCode, deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT);
cBuilder.setDeleteIntent(deletePendingIntent);

**/

Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), largeIcon);
Expand All @@ -189,7 +201,7 @@ public void notify_mailbox(PendingIntent pendingIntent, int smallIcon, int large

// 设置通知样式为收件箱样式,在通知中心中两指往外拉动,就能出线更多内容,但是很少见
//cBuilder.setNumber(messageList.size());
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
for (String msg : messageList) {
inboxStyle.addLine(msg);
}
Expand Down Expand Up @@ -226,19 +238,16 @@ public void notify_customview(RemoteViews remoteViews, PendingIntent pendingInte
* @param title
* @param content
*/
public void notify_normail_moreline(PendingIntent pendingIntent, int smallIcon, String ticker,
public void notify_normal_multiline(PendingIntent pendingIntent, int smallIcon, String ticker,
String title, String content, boolean sound, boolean vibrate, boolean lights) {

final int sdk = Build.VERSION.SDK_INT;
if (sdk < Build.VERSION_CODES.JELLY_BEAN) {
notify_normal_singline(pendingIntent, smallIcon, ticker, title, content, sound, vibrate, lights);
Toast.makeText(mContext, "您的手机低于Android 4.1.2,不支持多行通知显示!!", Toast.LENGTH_SHORT).show();
} else {
setBuilder(pendingIntent, smallIcon, ticker, true, true, false);
nBuilder.setContentTitle(title);
nBuilder.setContentText(content);
nBuilder.setPriority(Notification.PRIORITY_HIGH);
notification = new Notification.BigTextStyle(nBuilder).bigText(content).build();
setCompatBuilder(pendingIntent, smallIcon, ticker, title, content, sound, vibrate, lights);
notification = new Notification.BigTextStyle(cBuilder).bigText(content).build();
// 发送该通知
nm.notify(NOTIFICATION_ID, notification);
}
Expand All @@ -259,8 +268,8 @@ public void notify_progress(PendingIntent pendingIntent, int smallIcon,
setCompatBuilder(pendingIntent, smallIcon, ticker, title, content, sound, vibrate, lights);
/*
* 因为进度条要实时更新通知栏也就说要不断的发送新的提示,所以这里不建议开启通知声音。
* 这里是作为范例,给大家讲解下原理。所以发送通知后会听到多次的通知声音。
*/
* 这里是作为范例,给大家讲解下原理。所以发送通知后会听到多次的通知声音。
*/

new Thread(new Runnable() {
@Override
Expand Down Expand Up @@ -295,14 +304,12 @@ public void run() {
*/
public void notify_bigPic(PendingIntent pendingIntent, int smallIcon, String ticker,
String title, String content, int bigPic, boolean sound, boolean vibrate, boolean lights) {

setCompatBuilder(pendingIntent, smallIcon, ticker, title, null, sound, vibrate, lights);
NotificationCompat.BigPictureStyle picStyle = new NotificationCompat.BigPictureStyle();
Notification.BigPictureStyle picStyle = new Notification.BigPictureStyle();
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = true;
options.inSampleSize = 2;
Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(),
bigPic, options);
Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), bigPic, options);
picStyle.bigPicture(bitmap);
picStyle.bigLargeIcon(bitmap);
cBuilder.setContentText(content);
Expand Down
Binary file modified sample-debug.apk
Binary file not shown.
13 changes: 7 additions & 6 deletions sample/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
compileSdkVersion 26
buildToolsVersion "26.0.3"

defaultConfig {
applicationId "com.whee.wheetalklollipop"
minSdkVersion 15
targetSdkVersion 20
targetSdkVersion 26
versionCode 1
versionName "1.0"
}
Expand All @@ -22,9 +22,10 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.android.support:support-v13:23.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
compile 'com.android.support:appcompat-v7:26.1.0'
compile 'com.android.support:design:26.1.0'
compile 'com.android.support:support-v13:26.1.0'
compile 'com.commit451:PhotoView:1.2.4'
compile project(':library')
}
16 changes: 13 additions & 3 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.wenming.notify">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@drawable/logo"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/MyMaterialTheme">
android:theme="@style/MyMaterialTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity
android:name="com.wenming.notify.activity.MainActivity"
android:name=".activity.MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".activity.OtherActivity"></activity>
<activity android:name=".activity.OtherActivity" />
<activity
android:name=".notification.LaunchActivity"
android:launchMode="singleTask" />

<service
android:name=".notification.NotificationService"
android:exported="false" />
</application>

</manifest>
17 changes: 17 additions & 0 deletions sample/src/main/java/com/wenming/notify/MyApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.wenming.notify;

import android.app.Application;
import android.os.Build;

import com.wenming.notify.notification.NotificationChannels;

public class MyApplication extends Application {

@Override
public void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannels.createAllNotificationChannels(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.wenming.notify.adapter.ViewPagerAdapter;
import com.wenming.notify.fragment.OneFragment;
import com.wenming.notify.fragment.ProfileFragment;
import com.wenming.notify.fragment.TwoFragment;


public class MainActivity extends AppCompatActivity {
Expand Down Expand Up @@ -43,6 +44,7 @@ private void initTabViewPager() {
viewPager = (ViewPager) findViewById(R.id.viewpager);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new OneFragment(mContext), getString(R.string.tab2));
adapter.addFragment(new TwoFragment(), getString(R.string.tab4));
adapter.addFragment(new ProfileFragment(), getString(R.string.tab3));
viewPager.setAdapter(adapter);
tabLayout = (TabLayout) findViewById(R.id.tabs);
Expand Down
Loading