Android 消息的推送

今天学习了一下消息推送,分享一下!

主要的代码段:

 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>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值