三种推送消息的类型,一种是推送的消息直接在状态栏显示,单击没有任何跳转,可以清除。第二种同样在状态栏显示,单击有跳转到另一个界面的效果,像常用APP的消息推送,单击跳转activity,单击跳转后自动清除该推送。第三种就是显示APP正在运行的效果,单击回到APP主界面(也可以设置为跳转到上次记录的界面),该类型的推送不可清除。先看一下demo:


Android逐条发送消息 android消息推送_android



直接上代码:MainActivity的JAVA代码:

package com.example.statusbarshow;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;

@SuppressLint("NewApi")
public class MainActivity extends Activity {
	final int NOTIFYID_1 = 888;	//第一个通知的ID
	final int NOTIFYID_2 = 889;	//第二个通知的ID
	final int NOTIFYID_3 = 886;	//第三个通知的ID
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//PermisionUtils.verifyStoragePermissions(this);   //获取手机SD卡读取权限
		final NotificationManager notificationManager=     //获取通知管理器
				(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
		ImageButton i1=(ImageButton)findViewById(R.id.news1);         //获取定义的各个按键
		ImageButton i2=(ImageButton)findViewById(R.id.news2);
		Button b3=(Button)findViewById(R.id.news3);
		Button b4=(Button)findViewById(R.id.clear);
		
		i1.setOnClickListener(new OnClickListener() {     //推送第一种状态栏消息类型
			@Override
			public void onClick(View v) {
				Notification.Builder builder1 = new Notification.Builder(MainActivity.this);
				builder1.setSmallIcon(R.drawable.advise);       //设置图标
				builder1.setTicker("新消息");
				builder1.setWhen(System.currentTimeMillis()); //发送时间
				builder1.setContentTitle("推送消息1"); //设置标题
				builder1.setContentText("每天进步一点点就够了,切忌不思进取"); //消息内容
				builder1.setDefaults(Notification.DEFAULT_ALL);
				Notification notification1 = builder1.build();
				notificationManager.notify(NOTIFYID_1, notification1);// 通过通知管理器发送通知
				
				
			}
		});
		
		
		i2.setOnClickListener(new OnClickListener() {     //推送第一种状态栏消息类型
			@Override
			public void onClick(View v) {
				
				Notification.Builder builder1 = new Notification.Builder(MainActivity.this);
				builder1.setSmallIcon(R.drawable.advise1);       //设置图标
				builder1.setTicker("新消息");
				builder1.setWhen(System.currentTimeMillis()); //发送时间
				builder1.setContentTitle("推送消息2"); //设置标题
				builder1.setContentText("点击查看详情"); //消息内容
				builder1.setAutoCancel(true);//打开程序后图标消失
				builder1.setDefaults(Notification.DEFAULT_ALL);
				Intent intent=new Intent(MainActivity.this,SECOND.class); //设置点击跳转的activity
				PendingIntent pendingIntent=PendingIntent.getActivity(
						MainActivity.this, 0, intent, 0);
				builder1.setContentIntent(pendingIntent);
				Notification notification1 = builder1.build();
				notificationManager.notify(NOTIFYID_2, notification1);// 通过通知管理器发送通知
				
			}
		});
		
		b3.setOnClickListener(new OnClickListener() {     //推送第一种状态栏消息类型
			@Override
			public void onClick(View v) {
				
				Notification.Builder builder1 = new Notification.Builder(MainActivity.this);
				builder1.setSmallIcon(R.drawable.advise2);       //设置图标
				builder1.setTicker("新消息");
				builder1.setWhen(System.currentTimeMillis()); //发送时间
				builder1.setContentTitle("APP正在运行"); //设置标题
				builder1.setContentText("点击打开APP"); //消息内容
				builder1.setDefaults(Notification.DEFAULT_ALL);
				builder1.setOngoing(true);
				Intent intent=new Intent(MainActivity.this,MainActivity.class); //设置点击跳转的activity
				PendingIntent pendingIntent=PendingIntent.getActivity(
						MainActivity.this, 0, intent, 0);
				builder1.setContentIntent(pendingIntent);
				Notification notification1 = builder1.build();
				notificationManager.notify(NOTIFYID_3, notification1);// 通过通知管理器发送通知
							
			}
		});
		b4.setOnClickListener(new OnClickListener() {     //推送第一种状态栏消息类型
			@Override
			public void onClick(View v) {
				notificationManager.cancelAll();
				
			}
		});
	}

	
}

SECOND.JAVA代码:

package com.example.statusbarshow;

import android.app.Activity;
import android.os.Bundle;

public class SECOND extends Activity {
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
       setContentView(R.layout.second);
		
		}

}

布局文件activity_main代码:


<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background" 
    android:stretchColumns="0,3">

<TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_weight="1"
        >   
<LinearLayout
    android:baselineAligned="false"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" 
    android:gravity="bottom|center"
    android:layout_marginBottom="20dp"
    >
    <ImageButton
        android:id="@+id/news1"
        android:layout_width="130dp"
        android:layout_height="40dp"
        android:background="#0000"
        android:layout_marginRight="10dp" 
        android:src="@drawable/button_1"
        android:scaleType="fitXY"/>
     <ImageButton
        android:id="@+id/news2"
        android:layout_width="130dp"
        android:layout_height="40dp"
        android:background="#0000"
        android:src="@drawable/button_2"
        android:scaleType="fitXY"
         />
    </LinearLayout>
       </TableRow>
    

 <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:layout_weight="1">
<LinearLayout
    android:baselineAligned="false"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="top|center"
    android:orientation="horizontal" 
    >

     <Button
        android:id="@+id/news3"
        android:layout_width="130dp"
        android:layout_height="40dp"
        android:text="@string/news3"
        android:layout_marginRight="30dp" />
      <Button
        android:id="@+id/clear"
        android:background="#0000"
        android:textSize="25dp"
         android:layout_width="130dp"
        android:layout_height="40dp"
        android:text="@string/clear" />
    </LinearLayout>
    </TableRow>
</TableLayout>

布局文件SECOND代码:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是第二种推送消息类型,带有activity跳转的功能!!"
        android:textColor="#CC0033" />

</FrameLayout>

       单击按钮实现动画效果,通过建立xml文件,android:state_pressed="false"表示没有按下时按钮是加载背景图片news_1,按下按钮时加载图片news_2。图片放在res资源下的drawable下。最后在activity_main里面将imagebutton的背景src链接到该xml文件中即可实现按钮动画效果。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="false"
        android:drawable="@drawable/news_1"/>
        <item android:state_pressed="true"
        android:drawable="@drawable/news_2"/>
</selector>