今天学习了一下消息推送,分享一下!
主要的代码段:
button.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.O)//????????????????
@Override
public void onClick(View v) {
//创建通知建设类,获取通知栏管理
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//创建通知类
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
//设置通知栏标题
builder.setContentTitle("新邮件");
//设置通知栏内容
builder.setContentText("您有一封新邮件,点击查看。");
//设置图标
builder.setSmallIcon(R.mipmap.ic_launcher);
//设置
builder.setDefaults(Notification.DEFAULT_ALL);
//设置跳转的页面
/*
错误:No Channel found for pkg=com.example.xx.xx, channelId=null, id=1001, tag=null…
需要在发送通知前创建自定义通知渠道
*/
NotificationChannel channel = new NotificationChannel("001","my_channel",NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
builder.setChannelId("001");
PendingIntent intent = PendingIntent.getActivity(MainActivity.this,
1, new Intent(MainActivity.this, SecondActivity.class),
PendingIntent.FLAG_CANCEL_CURRENT);
//设置跳转
builder.setContentIntent(intent);
Notification notification = builder.build();
//显示在通知栏
manager.notify(0, notification);
}
});
完整代码如下:
1、MainActivity
public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=findViewById(R.id.ids);
button.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.O)//????????????????
@Override
public void onClick(View v) {
//创建通知建设类//获取通知栏管理
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//创建通知类
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
//设置通知栏标题
builder.setContentTitle("新邮件");
//设置通知栏内容
builder.setContentText("您有一封新邮件,点击查看。");
//设置图标
builder.setSmallIcon(R.mipmap.ic_launcher);
//设置
builder.setDefaults(Notification.DEFAULT_ALL);
//设置跳转的页面
/*
错误:No Channel found for pkg=com.example.xx.xx, channelId=null, id=1001, tag=null…
需要在发送通知前创建自定义通知渠道
*/
NotificationChannel channel = new NotificationChannel("001","my_channel",NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
builder.setChannelId("001");
PendingIntent intent = PendingIntent.getActivity(MainActivity.this,
1, new Intent(MainActivity.this, SecondActivity.class),
PendingIntent.FLAG_CANCEL_CURRENT);
//设置跳转
builder.setContentIntent(intent);
Notification notification = builder.build();
//显示在通知栏
manager.notify(0, notification);
}
});
}
}
2、activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ids"
android:text="通知"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"/>
</RelativeLayout>
3、SecondActivity:
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_main);
}
}
4、second_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:text="你好,你在哪里??????"/>
</RelativeLayout>
5、manifest:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity">
</activity>
</application>