短信推送通知

Posted

技术标签:

【中文标题】短信推送通知【英文标题】:SMS push-Notification 【发布时间】:2012-03-05 10:50:47 【问题描述】:

我想创建一个应用程序,它在使用广播接收器接收或发送短信时发出通知。怎么做?谢谢。

【问题讨论】:

和***.com/questions/4117701/…有区别吗? 【参考方案1】:

试试这个 公共类 smsActivity 扩展 BroadcastReceiver

        protected static final String TAG = "PlayingAudio";



        @Override
        public void onReceive(Context context, Intent intent) 
        
             this.mContext = context;
             mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
             mFileName += "/audiorecordtest.3gp";
            //---get the SMS message passed in---
            Bundle bundle = intent.getExtras();        

            if (bundle != null) 
            
                //---retrieve the SMS message received---
                Object[] pdus = (Object[]) bundle.get("pdus");
                msgs = new SmsMessage[pdus.length];            
                for (int i=0; i<msgs.length; i++)
                    msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);   
                    incomingnum=msgs[i].getOriginatingAddress();
                    MsgBody=msgs[i].getMessageBody().toString();
                    //str += "SMS from " +incomingnum;                     
                    //str += " :";
                    str += MsgBody;
                   // str += "\n";        
                    System.out.println("Str is   "+str);
                 
Notification notification = new Notification(android.R.drawable.ic_popup_reminder,
                            "My Notification", System.currentTimeMillis());
                    notification.defaults |= Notification.DEFAULT_SOUND;
                    notification.defaults |= Notification.DEFAULT_VIBRATE;*/ 

                
            

        

    

【讨论】:

【参考方案2】:

在运行 API 7 映像的模拟器中测试。

接收器类:

package add.yourpackage.here;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;

public class SmsReceiver extends BroadcastReceiver 
    @Override
    public void onReceive(Context context, Intent intent) 
        Bundle bundle = intent.getExtras();

        if (bundle != null) 
            StringBuilder smsSummary = new StringBuilder("SMS received from: ");

            Object[] pdus = (Object[]) bundle.get("pdus");
            SmsMessage[] msgs = new SmsMessage[pdus.length];
            for (int i = 0; i < msgs.length; i++) 
                msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);

                final String sender = msgs[i].getOriginatingAddress();
                final String message = msgs[i].getMessageBody().toString();
                smsSummary.append(sender);
                smsSummary.append("; ").append(message).append("\n");

                Log.d("SMS_RECEIVER", "Str is   " + smsSummary);
            

            NotificationManager notifManager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);

            Notification notif = new Notification(
                    android.R.drawable.ic_popup_reminder, smsSummary,
                    System.currentTimeMillis());
            notif.defaults |= Notification.DEFAULT_SOUND;
            notif.defaults |= Notification.DEFAULT_VIBRATE;
            notif.defaults |= Notification.DEFAULT_LIGHTS;

            // The notification will be canceled when clicked by the user...

            notif.flags |= Notification.FLAG_AUTO_CANCEL;

            // ...but we still need to provide and intent; an empty one will
            // suffice. Alter for your own app's requirement.

            Intent notificationIntent = new Intent();
            PendingIntent pi = PendingIntent.getActivity(context, 0,
                    notificationIntent, 0);
            notif.setLatestEventInfo(context, "SMS Notification",
                    "Another txt for you", pi);

            notifManager.notify(0, notif);
        
    

如果您运行的是 API 11+,则可以改用 Notification.Builder,但我的手机仍在 Gingerbread 上。

接收方的清单条目。您的应用仍需要定义权限才能接收 SMS 消息,但我假设您已经这样做了:

    <receiver
        android:name=".service.SmsReceiver"
        android:enabled="true" >
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            <action android:name="android.intent.action.DATA_SMS_RECEIVED" />
        </intent-filter>
    </receiver>

【讨论】:

没关系,这段代码也适用于 API 8 :-)。我只在 7 上进行了测试,因为它是我安装的最低 API,而您在原始问题中没有说明。

以上是关于短信推送通知的主要内容,如果未能解决你的问题,请参考以下文章

如果被短信、推送通知或电池电量不足等 uialert 中断,如何自动继续录制

苹果推送通知希伯来语

即使应用程序关闭,whatsapp 如何设法接收推送通知?

Windows Mobile 6.0/6.5 - 推送通知

消息通知系统模型设计

Swift导航到View from notification