GVKun编程网logo

当有人发表评论时,如何使用django-notification通知用户

29

这篇文章主要围绕当有人发表评论时,如何使用django-notification通知用户展开,旨在为您提供一份详细的参考资料。我们将全面介绍当有人发表评论时,如何使用django-notificati

这篇文章主要围绕当有人发表评论时,如何使用django-notification通知用户展开,旨在为您提供一份详细的参考资料。我们将全面介绍当有人发表评论时,如何使用django-notification通知用户,同时也会为您带来android Notification 的一个简单应用(在 Notification 中嵌入一个进度条,并且这个 Notification 点击消失但不会跳转)、Android Notification通知使用详解、Android Notification通知详解、android notification,notificationmanager详解的实用方法。

本文目录一览:

当有人发表评论时,如何使用django-notification通知用户

当有人发表评论时,如何使用django-notification通知用户

我已经在django进行了一段时间的开发,并且已经开发了一个简洁的网站,该网站具有编写博客,发布问题,共享内容等功能。但是,仍然缺少一件事,即为用户创建通知。

我想做的是,只要有人对自己的帖子发表评论,或者有人关注某个特定的帖子并且有最新动态,便会在其个人资料中通知用户,然后将该更新告知用户。我环顾了许多应用程序,但是我对如何做仍然感到困惑。

在使用的情况下,django-notification我似乎有一种印象(可能是错误的),我只能用它来通过电子邮件通知用户,即我无法像在Facebook上一样在用户个人资料中显示这些通知。

首先,我想知道我是否做错了,然后我真的需要一些适当的教程或指导来进行操作。我知道如何注册通知并通过适当的信号发送通知,但是如果可以的话,没有文档说明如何在模板中显示这些通知。

任何指导/教程/入门文档将不胜感激。

答案1

小编典典

是的django-notifications仅用于电子邮件通知。

这是一个信号槽,您可以将其添加到models.py中并根据自己的需要进行调整:

from django.db import modelsfrom django.contrib.sites.models import Sitefrom django.db.models import signalsfrom notification import models as notificationdef create_notice_types(app, created_models, verbosity, **kwargs):    notification.create_notice_type("new_comment", "Comment posted", "A comment has been posted")signals.post_syncdb.connect(create_notice_types, sender=notification)def new_comment(sender, instance, created, **kwargs):    # remove this if-block if you want notifications for comment edit too    if not created:        return None    context = {        ''comment'': instance,        ''site'': Site.objects.get_current(),    }    recipients = []    # add all users who commented the same object to recipients    for comment in instance.__class__.objects.for_model(instance.content_object):        if comment.user not in recipients and comment.user != instance.user:            recipients.append(comment.user)    # if the commented object is a user then notify him as well    if isinstance(instance.content_object, models.get_model(''auth'', ''User'')):        # if he his the one who posts the comment then don''t add him to recipients        if instance.content_object != instance.user and instance.content_object not in recipients:            recipients.append(instance.content_object)    notification.send(recipients, ''new_comment'', context)signals.post_save.connect(new_comment, sender=models.get_model(''comments'', ''Comment''))

现在使用模板,非常简单。

模板/通知/new_comment/short.txt

{{ comment.user }} commented on {{ comment.object }}

模板/通知/new_comment/notice.html

<a href="{{ comment.user.get_absolute_url }}">{{ comment.user }}</a> commented <a href="{{ comment.content_object.get_absolute_url }}">{{ comment.content_object }}</a>

模板/通知/new_comment/full.txt

{{ comment.user }} commented on {{ comment.content_object }}Comment:{{ comment.comment }}Reply on: http://{{ site.domain }}{{ comment.content_object.get_absolute_url }}

警告:这是对我们的生产代码的非常简化且未经测试的修改。

注意: Django-1.7已弃用post_syncdb信号

以下是更多信息:

  • 文件资料
  • stringfellow的博客文章

android Notification 的一个简单应用(在 Notification 中嵌入一个进度条,并且这个 Notification 点击消失但不会跳转)

android Notification 的一个简单应用(在 Notification 中嵌入一个进度条,并且这个 Notification 点击消失但不会跳转)

   网上很多的例子都是直接获取 Notification 对象来设置一个通知,其实 Notification 跟 Dialog 一样,也有自己的 Builder,可以用 builder 对象来设置一个 Notification

    这个例子是在 Notification 中嵌入一个进度条,并且这个 Notification 点击消失但不会跳转(跟 android 的 vcard 文件导入时弹出的 Notification 一样)

    NotificationManager mNotificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification.Builder builder = new Notification.Builder(context);
        builder.setOngoing(true);
        builder.setProgress (total, current, false);// 设置进度条,false 表示是进度条,true 表示是个走马灯
        builder.setTicker (title);// 设置 title
        builder.setWhen(System.currentTimeMillis());
        builder.setContentTitle (content);// 设置内容
        builder.setAutoCancel (true);// 点击消失
        builder.setSmallIcon(R.drawable.upload);
        builder.setContentIntent (PendingIntent.getActivity (context, 0, new Intent (), 0));// 这句和点击消失那句是 “Notification 点击消失但不会跳转” 的必须条件,如果只有点击消失那句,这个功能是不能实现的

        Notification noti = builder.getNotification();
        mNotificationManager.notify(id,noti);

希望这个例子对其他人有点用,因为我特曾为这个功能苦恼过,呵呵!


Android Notification通知使用详解

Android Notification通知使用详解

在Android应用的开发中,必然会遇上通知的开发需求,本文主要讲一下Android中的通知 Notification的简单基本使用,主要包含创建通知渠道、初始化通知、显示通知、显示图片通知、通知点击、以及配合WorkManager发送延迟通知。

Demo下载

创建通知渠道

首先,创建几个常量和变量,其中渠道名是会显示在手机设置-通知里app对应展示的通知渠道名称,一般基于通知作用取名。

    companion object {
        //渠道Id
        private const val CHANNEL_ID = "渠道Id"
        //渠道名
        private const val CHANNEL_NAME = "渠道名-简单通知"
        //渠道重要级
        private const val CHANNEL_IMPORTANCE = NotificationManager.IMPORTANCE_DEFAULT
    }
    private lateinit var context: Context
    //Notification的ID
    private var notifyId = 100
    private lateinit var manager: NotificationManager
    private lateinit var builder: NotificationCompat.Builder

然后获取系统通知服务,创建通知渠道,其中因为通知渠道是Android8.0才有的,所以增加一个版本判断:

        //获取系统通知服务
        manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        //创建通知渠道,Android8.0及以上需要
        createChannel()
    private fun createChannel() {
        //创建通知渠道,Android8.0及以上需要
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            return
        }
        val notificationChannel = NotificationChannel(
            CHANNEL_ID,
            CHANNEL_NAME,
            CHANNEL_IMPORTANCE
        )
        manager.createNotificationChannel(notificationChannel)
    }

初始化通知

先生成NotificationCompat.Builder,然后初始化通知Builder的通用配置:

        builder = NotificationCompat.Builder(context.applicationContext, CHANNEL_ID)
        initNotificationBuilder()
    /**
     * 初始化通知Builder的通用配置
     */
    private fun initNotificationBuilder() {
        builder
            .setAutoCancel(true) //设置这个标志当用户单击面板就可以让通知自动取消
            .setSmallIcon(R.drawable.ic_reminder) //通知的图标
            .setWhen(System.currentTimeMillis()) //通知产生的时间,会在通知信息里显示
            .setDefaults(Notification.DEFAULT_ALL)
    }

此外builder还有setVibrate、setSound、setStyle等方法,按需配置即可。

显示通知

给builder设置需要通知需要显示的title和content,然后通过builder.build()生成生成通知Notification,manager.notify()方法将通知发送出去。

    fun configNotificationAndSend(title: String, content: String){
        builder.setContentTitle(title)
            .setContentText(content)
        val notification = builder.build()
        //发送通知
        manager.notify(notifyId, notification)
        //id自增
        notifyId++
    }

最简单的通知显示至此上面三步就完成了。

效果如下图:

显示图片通知

当通知内容过多一行展示不下时,可以通过设置

builder.setStyle(NotificationCompat.BigTextStyle().bigText(content)) //设置可以显示多行文本

这样通知就能收缩和展开,显示多行文本。

另外setStyle还可以设置图片形式的通知:

setStyle(NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(resources,R.drawable.logo)))//设置图片样式

效果如下图:

通知点击

目前为止的通知还只是显示,因为设置了builder.setAutoCancel(true),点击通知之后通知会自动消失,除此之外还没有其他操作。

给builder设置setContentIntent(PendingIntent)就能有通知点击之后的其他操作了。PendingIntent可以看作是对Intent的一个封装,但它不是立刻执行某个行为,而是满足某些条件或触发某些事件后才执行指定的行为。PendingIntent获取有三种方式:Activity、Service和BroadcastReceiver获取。通过对应方法PendingIntent.getActivity、PendingIntent.getBroadcast、PendingIntent.getService就能获取。

这里就示例一下PendingIntent.getBroadcast和PendingIntent.getActivity

PendingIntent.getBroadcast

首先创建一个BroadcastReceiver:

class NotificationHandleReceiver : BroadcastReceiver() {
    companion object {
        const val NOTIFICATION_HANDLE_ACTION = "notification_handle_action"
        const val NOTIFICATION_LINK = "notificationLink"
        const val TAG = "NotificationReceiver"
    }
    override fun onReceive(context: Context, intent: Intent?) {
        if (intent?.action == NOTIFICATION_HANDLE_ACTION) {
            val link = intent.getStringExtra(NOTIFICATION_LINK)
        }
    }
}

别忘了在清单文件中还需要静态注册BroadcastReceiver:

    <receiver
        android:name=".NotificationHandleReceiver"
        android:exported="false">
        <intent-filter>
            <action android:name="notification_handle_action" />
        </intent-filter>
    </receiver>

然后创建一个上面BroadcastReceiver的Intent,在intent.putExtra传入相应的点击通知之后需要识别的操作:

   fun generateDefaultBroadcastPendingIntent(linkParams: (() -> String)?): PendingIntent {
        val intent = Intent(NotificationHandleReceiver.NOTIFICATION_HANDLE_ACTION)
        intent.setPackage(context.packageName)
        linkParams?.let {
            val params = it.invoke()
            intent.putExtra(NotificationHandleReceiver.NOTIFICATION_LINK, params)
        }
        return PendingIntent.getBroadcast(
            context,
            notifyId,
            intent,
            PendingIntent.FLAG_IMMUTABLE
        )
    }

这样生成的PendingIntent再builder.setContentIntent(pendingIntent),在我们点击通知之后,NotificationHandleReceiver的onReceive里就会收到信息了,根据信息处理后续操作即可。

PendingIntent.getActivity

Activity的PendingIntent用于跳转到指定activity,创建一个跳转activity的Intent(同普通的页面跳转的Intent),也是同上面在intent.putExtra传入相应的点击通知之后需要识别的操作:

        val intent = Intent(this, XXXX::class.java).apply {
            putExtra("title", title).putExtra("content", content)
        }
        return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)

也是这样生成的PendingIntent再builder.setContentIntent(pendingIntent),在我们点击通知之后,就会跳转到对应的activity页面,然后intent里就会收到信息了,根据信息处理后续操作即可。

Android12之PendingIntent特性

行为变更:以 Android 12 为目标平台的应用

查看上面关于Android12的特性

在Android12平台上有关于PendingIntent的两点特性:

一是待处理 intent 可变性,必须为应用创建的每个PendingIntent对象指定可变性,这也是上面创建PendingIntent时需要设置flag为PendingIntent.FLAG_IMMUTABLE。

二是通知 trampoline 限制,以 Android 12 或更高版本为目标平台的应用无法从用作通知 trampoline 的服务或广播接收器中启动 activity。换言之,当用户点按通知或通知中的操作按钮时,您的应用无法在服务或广播接收器内调用startActivity()。所以当需要点击通知实现activity跳转时,需要使用PendingIntent. getActivity,而不是使用PendingIntent.getBroadcast,然后在BroadcastReceiver里实现activity跳转,后者方式在Android 12 或更高版本为目标平台的应用中将被限制。

配合WorkManager发送延迟通知

配合上WorkManager,就能实现发送延迟通知,主要是通过OneTimeWorkRequest的延迟特性。

创建一个延迟的OneTimeWorkRequest,加入WorkManager队列中:

    fun sendWorkRequest(
        context: Context,
        reminderId: Int,
        title: String,
        content: String,
        link: String,
        triggerTime: Long
    ): OneTimeWorkRequest {
        val duration = triggerTime - System.currentTimeMillis()
        val data =
            Data.Builder().putInt(REMINDER_WORKER_DATA_ID, reminderId).putString(REMINDER_WORKER_DATA_TITLE, title)
                .putString(REMINDER_WORKER_DATA_CONTENT, content).putString(REMINDER_WORKER_DATA_LINK, link)
                .build()
        val uniqueWorkName =
            "reminderData_${reminderId}"
        val request = OneTimeWorkRequest.Builder(ReminderWorker::class.java)
            .setInitialDelay(duration, TimeUnit.MILLISECONDS)
            .setInputData(data)
            .build()
        WorkManager.getInstance(context)
            .enqueueUniqueWork(uniqueWorkName, ExistingWorkPolicy.REPLACE, request)
        return request
    }

然后在doWork方法中拿到数据进行我们上面的通知发送显示即可。具体关于OneTimeWorkRequest的使用在本文中就不详细说明了。当需要发送延迟通知时,知道可以通过配合WorkManager实现。

Android13 通知权限

在目前最新的Android 13(API 级别 33)上对于通知增加了权限限制,具体可看官方描述:

通知运行时权限

到此这篇关于Android Notification通知使用详解的文章就介绍到这了,更多相关Android Notification内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

您可能感兴趣的文章:
  • Android使用Notification实现通知功能
  • Android开发之Notification手机状态栏通知用法实例分析
  • Android使用Notification在状态栏上显示通知
  • Android中Notification通知用法详解
  • Android 中Notification弹出通知实现代码
  • Android中使用Notification实现状态栏的通知
  • android 通知Notification详解及实例代码
  • Android开发之Notification通知用法详解
  • Android中通知Notification的使用方法

Android Notification通知详解

Android Notification通知详解

Android Notification通知详解

根据activity的生命周期,在activity不显示时,会执行onStop函数(比如按下home键),所以你在onStop函数(按退出键除外)里面把notification放在通知栏里,再此显示时,把notification从通知栏里去掉。或者,只要程序在运行就一直显示通知栏图标。

 

下面对Notification类中的一些常量,字段,方法简单介绍一下:
常量:
DEFAULT_ALL 使用所有默认值,比如声音,震动,闪屏等等
DEFAULT_LIGHTS 使用默认闪光提示
DEFAULT_SOUNDS 使用默认提示声音
DEFAULT_VIBRATE 使用默认手机震动
【说明】:加入手机震动,一定要在manifest.xml中加入权限:

以上的效果常量可以叠加,即通过
notification.defaults =DEFAULT_SOUND|DEFAULT_VIBRATE;
notification.defaults |= DEFAULT_SOUND (最好在真机上测试,震动效果模拟器上没有)

 

//设置flag位
FLAG_AUTO_CANCEL 该通知能被状态栏的清除按钮给清除掉
FLAG_NO_CLEAR 该通知能被状态栏的清除按钮给清除掉
FLAG_ONGOING_EVENT 通知放置在正在运行
FLAG_INSISTENT 是否一直进行,比如音乐一直播放,知道用户响应

 

常用字段:
contentIntent 设置PendingIntent对象,点击时发送该Intent
defaults 添加默认效果
flags 设置flag位,例如FLAG_NO_CLEAR等
icon 设置图标
sound 设置声音
tickerText 显示在状态栏中的文字
when 发送此通知的时间戳

 

notificationmanager常用方法介绍:
public void cancelAll() 移除所有通知(只是针对当前Context下的Notification)
public void cancel(int id) 移除标记为id的通知 (只是针对当前Context下的所有Notification)
public void notify(String tag,int id,Notification notification) 将通知加入状态栏,标签为tag,标记为id
public void notify(int id,Notification notification) 将通知加入状态栏,标记为id

 

?
package com.ljq.activity;

import android.app.Activity;
import android.app.Notification;
import android.app.notificationmanager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;

public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
clearNotification();
}

@Override
protected void onStop() {
showNotification();
super.onStop();
}

@Override
protected void onStart() {
clearNotification();
super.onStart();
}

/**
* 在状态栏显示通知
*/
private void showNotification(){
// 创建一个notificationmanager的引用
notificationmanager notificationmanager = (notificationmanager)
this.getSystemService(android.content.Context.NOTIFICATION_SERVICE);

// 定义Notification的各种属性
Notification notification =new Notification(R.drawable.icon,
"督导系统",System.currentTimeMillis());
//FLAG_AUTO_CANCEL 该通知能被状态栏的清除按钮给清除掉
//FLAG_NO_CLEAR 该通知不能被状态栏的清除按钮给清除掉
//FLAG_ONGOING_EVENT 通知放置在正在运行
//FLAG_INSISTENT 是否一直进行,比如音乐一直播放,知道用户响应
notification.flags |= Notification.FLAG_ONGOING_EVENT; // 将此通知放到通知栏的"Ongoing"即"正在运行"组中
notification.flags |= Notification.FLAG_NO_CLEAR; // 表明在点击了通知栏中的"清除通知"后,此通知不清除,经常与FLAG_ONGOING_EVENT一起使用
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
//DEFAULT_ALL 使用所有默认值,比如声音,震动,闪屏等等
//DEFAULT_LIGHTS 使用默认闪光提示
//DEFAULT_SOUNDS 使用默认提示声音
//DEFAULT_VIBRATE 使用默认手机震动,需加上权限
notification.defaults = Notification.DEFAULT_LIGHTS;
//叠加效果常量
//notification.defaults=Notification.DEFAULT_LIGHTS|Notification.DEFAULT_SOUND;
notification.ledARGB = Color.BLUE;
notification.ledOnMS =5000; //闪光时间,毫秒

// 设置通知的事件消息
CharSequence contentTitle ="督导系统标题"; // 通知栏标题
CharSequence contentText ="督导系统内容"; // 通知栏内容
Intent notificationIntent =new Intent(MainActivity.this,MainActivity.class); // 点击该通知后要跳转的Activity
PendingIntent contentItent = PendingIntent.getActivity(this,notificationIntent,0);
notification.setLatestEventInfo(this,contentTitle,contentText,contentItent);

// 把Notification传递给notificationmanager
notificationmanager.notify(0,notification);
}
?
//删除通知
private void clearNotification(){
// 启动后删除之前我们定义的通知
notificationmanager notificationmanager = (notificationmanager) this
.getSystemService(NOTIFICATION_SERVICE);
notificationmanager.cancel(0);

}
}

android notification,notificationmanager详解

android notification,notificationmanager详解

我们知道在使用Android的通知的时候一定会用到NotificationManager 、 Notification这两个类,这两个类的作用分别是

NotificationManager :  是状态栏通知的管理类,负责发通知、清楚通知等。

Notification:状态栏通知对象,可以设置icon、文字、提示声音、振动等等参数。

这里需要声明一点,由于Android的系统升级,Android在通知这块也有很多老的东西被抛弃了,一个是api11的版本,一个是api16的版本。我们来比较下api11之前的用法这是通用的:

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,  
                    new Intent(this, MainActivity.class), 0);  
            // 下面需兼容Android 2.x版本是的处理方式   
            Notification notify1 = new Notification();  
            notify1.icon = R.drawable.message;  
            notify1.tickerText = "TickerText:您有新短消息,请注意查收!";  
            notify1.when = System.currentTimeMillis();  
            notify1.setLatestEventInfo(this, "Notification Title",  
                    "This is the notification message", pendingIntent);  
            notify1.number = 1;  
            notify1.flags |= Notification.FLAG_AUTO_CANCEL; 
            manager.notify(NOTIFICATION_FLAG, notify1);  
api11-api16的用法是这样的(主要是新增了自定义通知图标,并且通知的构造方式也发生了改变)

 PendingIntent pendingIntent2 = PendingIntent.getActivity(this, 0,  
                    new Intent(this, MainActivity.class), 0);  
            // API11之后才支持  
            Notification notify2 = new Notification.Builder(this)  
                    .setSmallIcon(R.drawable.message) 
                    .setTicker("TickerText:" + "您有新短消息,请注意查收!") 
                    .setContentTitle("Notification Title")  
                    .setContentText("This is the notification message")
                    .setContentIntent(pendingIntent2)
                    .setNumber(1) 
                    .getNotification(); // 需要注意build()是在API level  
            // 16及之后增加的,在API11中可以使用getNotificatin()来代替  
            notify2.flags |= Notification.FLAG_AUTO_CANCEL;  
            manager.notify(NOTIFICATION_FLAG, notify2);  

api16之后

PendingIntent pendingIntent3 = PendingIntent.getActivity(this, 0,  
                    new Intent(this, MainActivity.class), 0);  
            // API16之后才支持  
            Notification notify3 = new Notification.Builder(this)  
                    .setSmallIcon(R.drawable.message)  
                    .setTicker("TickerText:" + "您有新短消息,请注意查收!")  
                    .setContentTitle("Notification Title")  
                    .setContentText("This is the notification message")  
                    .setContentIntent(pendingIntent3).setNumber(1).build(); 
            notify3.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。  
            manager.notify(NOTIFICATION_FLAG, notify3);//关联通知

我们这里讲的主要是api16之后的使用方法

首先我们通过系统的Service获取NotificationManager对象,然后通过他将消息发送给系统,获取方法如下:

NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  

Notification主要包含以下参数:

  • An icon  (通知的图标)
  • A title and expanded message  (通知的标题和内容)
  • PendingIntent   (点击通知执行页面跳转)
使用流程:

1、创建NotificationManager 

通过NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);获取NotificationNotificationManager 消息管理类,

2,创建Notification实体

通过Notification.Builder builder = new Notification.Builder(this);创建一个通知的实体,里面可以包含很多的参数,如通知的Icon,消息内容,跳转等。

3,通过notificationManager.notify(0, builder.build());将消息绑定,里面会用到NotificationService(这里不做讲解)

普通通知

Notification.Builder builder = new Notification.Builder(this);
        Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://blog.csdn.net/xiangzhihong8"));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0);
        builder.setContentIntent(pendingIntent);
        builder.setSmallIcon(R.drawable.lanucher);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.lanucher));
        builder.setAutoCancel(true);
        builder.setContentTitle("普通通知");
        builder.setContentText("您有新短消息,请注意查收");
        notificationManager.notify(0, builder.build());
折叠式通知

我们还可以通过RemoteViews(这里就是桌面小控件的实现,不知道大家是否还有印象)

Notification.Builder builder = new Notification.Builder(this);
        Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://blog.csdn.net/itachi85/"));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0);
        builder.setContentIntent(pendingIntent);
        builder.setSmallIcon(R.drawable.foldleft);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.lanucher));
        builder.setAutoCancel(true);
        builder.setContentTitle("折叠菜单");
        builder.setContentText("您有新短消息,请注意查收");
        //用RemoteViews来创建自定义Notification视图
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.view_fold);
        Notification notification = builder.build();
        //指定展开时的视图
        notification.bigContentView = remoteViews;
        notificationManager.notify(1, notification);

自定义通知

  Notification.Builder builder = new Notification.Builder(this);
        Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://blog.csdn.net/xiangzhihong8"));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0);
        builder.setContentIntent(pendingIntent);
        builder.setSmallIcon(R.drawable.foldleft);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon));
        builder.setAutoCancel(true);
        builder.setContentTitle("自定义菜单");
        builder.setContentText("您有新短消息,请注意查收");
        //设置点击跳转
        Intent hangIntent = new Intent(this,NotificationActivity.class);
        hangIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent hangPendingIntent = PendingIntent.getActivity(this, 0, hangIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        builder.setFullScreenIntent(hangPendingIntent, true);

        notificationManager.notify(2, builder.build());


源码: http://download.csdn.net/detail/xiangzhihong8/9639345


本文同步分享在 博客“xiangzhihong8”(CSDN)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

今天关于当有人发表评论时,如何使用django-notification通知用户的介绍到此结束,谢谢您的阅读,有关android Notification 的一个简单应用(在 Notification 中嵌入一个进度条,并且这个 Notification 点击消失但不会跳转)、Android Notification通知使用详解、Android Notification通知详解、android notification,notificationmanager详解等更多相关知识的信息可以在本站进行查询。

本文标签: