Android notification 例子

1.定义IntentService,接收后台notification消息,从而触发。

public class GCMIntentService extends GCMBaseIntentService {

    public GCMIntentService() {
        super("");
    }
   
    @Override
    protected void onMessage(Context context, Intent intent) {
        String message = intent.getStringExtra("message");
        String url = intent.getStringExtra("url");

        generateNotification(getString(R.string.app_name), message, url);
    }
   
    private void generateNotification(String title, String content, String url) {
        long when = System.currentTimeMillis();
        Intent notifyIntent = new Intent(this, MainActivity.class);
        notifyIntent.putExtra(ConstantUtil.BUNDLE_KEY_TITLE, title);
        notifyIntent.putExtra(ConstantUtil.BUNDLE_KEY_CONTENT, content);
        notifyIntent.putExtra(ConstantUtil.BUNDLE_KEY_WEBURL, url);
        notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.icon)
                        .setContentTitle(title)
                        .setContentText(content)
                        .setContentIntent(pendingIntent)
                        .setAutoCancel(true)
                        .setDefaults(Notification.DEFAULT_SOUND)
                        .setWhen(when);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify((int) when, builder.build());
    }
}
当后台接收到消息时,会触发这个service,onMessage会调用,然后产生一个notification,用户点击后跳转到MainActivity。


2.定义MainActivity接收notification的数据,然后以对话框的形式弹出。

public class MainActivity extends Activity{
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //handle notification when app at background
        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            final String title = bundle.getString(ConstantUtil.BUNDLE_KEY_TITLE);
            final String content = bundle.getString(ConstantUtil.BUNDLE_KEY_CONTENT);
            final String url = bundle.getString(ConstantUtil.BUNDLE_KEY_WEBURL);
            if (!StringUtil.isEmpty(content))
                showNotificationDialog(title, content, url);
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        //handld notification when app at foreground
        Bundle bundle = intent.getExtras();
        if (null != bundle) {
            final String title = bundle.getString(ConstantUtil.BUNDLE_KEY_TITLE);
            final String content = bundle.getString(ConstantUtil.BUNDLE_KEY_CONTENT);
            final String url = bundle.getString(ConstantUtil.BUNDLE_KEY_WEBURL);

            if (!StringUtil.isEmpty(content))
                showNotificationDialog(title, content, url);
        }
    }
}
当应用运行在后台的时候,notification到达时,会调用onCreate(),当应用在前端显示时,notification到达时,会调用onNewIntent(),所以需要单独处理每一种情况,此外必须注意的是onNewIntent()方法,代码:

        Bundle bundle = intent.getExtras();
//      Bundle bundle = getIntent().getExtras();

不能使用getIntent()的方法去取notification传过来的数据,getIntent()取到的intent是之前的intent,所以应该使用onNewIntent(Intent intent)的intent去获取数据。


3.AndroidManifest文件里定义:

<activity android:name=".MainActivity"
                  android:alwaysRetainTaskState="true"
                  android:launchMode="singleTask"
                  android:taskAffinity=""
                  android:excludeFromRecents="true"
                  android:screenOrientation="portrait"/>
<service android:name=".GCMIntentService"/>
大致流程就是如此了。



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值